I have a search form that is fired from my grid view select button. It works fine when it loads as in the fields fill up with the selected row details as expected but when I change any field and click on update record it does not take the updated values that I just typed into the text box but seems to resave the original values . Please help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Search_Reporters : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Ingest;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
//txt_rname.Enabled = false;
// txt_rmobile.Enabled = false;
// txt_remail.Enabled = false;
con.Open();
// string qry = "select * from Reporter where Reporter_ID= " + DropDownList1.SelectedValue + "";
lbl_1.Text = Session["id"].ToString();
string qry = "select * from Reporter where Reporter_ID= " + Session["id"] + " ";
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt); // table data-> data table
con.Close();
txt_rname.Text = dt.Rows[0][1].ToString();
txt_remail.Text = dt.Rows[0][2].ToString();
txt_rmobile.Text = dt.Rows[0][3].ToString();
}
protected void btn_edit_Click(object sender, EventArgs e)
{
txt_rname.Enabled = true;
txt_rmobile.Enabled = true;
txt_remail.Enabled = true;
lbl_remark.Text = "";
}
protected void btn_Save_Click(object sender, EventArgs e)
{
con.Open();
string qry = "update Reporter set Reporter_Name ='" + txt_rname.Text + "',Reporter_Email='" + txt_remail.Text + "',Reporter_Mobile = '" + txt_rmobile.Text + "' where Reporter_ID = " +lbl_1.Text+ "";
SqlCommand cmd = new SqlCommand(qry, con);
int x = cmd.ExecuteNonQuery();
if (x == 1)
{
//lbl_remark.Text = "Updated Successfully";
Response.Redirect("Reporters_remarks.aspx");
}
else
{
lbl_remark.Text = "error";
}
con.Close();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
lbl_remark.Text = "";
txt_rname.Text = "";
txt_remail.Text = "";
txt_rmobile.Text = "";
con.Open();
string qry = "select * from Reporter where Reporter_ID= " + DropDownList1.SelectedValue + "";
//string qry = "select * from Reporter where Reporter_ID= "+Session["id"]+" ";
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt); // table data-> data table
con.Close();
if (dt.Rows.Count == 0)
{
lbl_remark.Text = "User Not Found";
}
else
{
txt_rname.Text = dt.Rows[0][1].ToString();
txt_remail.Text = dt.Rows[0][2].ToString();
txt_rmobile.Text = dt.Rows[0][3].ToString();
}
}
}
HTML Code
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Reporters_Search.aspx.cs" Inherits="Search_Reporters" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
}
.style2
{
text-align: center;
}
.style3
{
text-align: right;
width: 648px;
}
.style4
{
text-align: center;
width: 648px;
}
.style5
{
text-decoration: underline;
font-size: larger;
}
.style6
{
text-align: center;
height: 17px;
}
.style7
{
height: 17px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td colspan="2" style="text-align: center" class="style5">
<strong>Reporter Search</strong></td>
</tr>
<tr>
<td class="style4">
</td>
<td>
</td>
</tr>
<tr>
<td class="style4" style="text-align: right">
Reporter ID:</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="Reporter_ID"
DataValueField="Reporter_ID"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:IngestConnectionString %>"
SelectCommand="SELECT [Reporter_ID] FROM [Reporter] ORDER BY [Reporter_ID]">
</asp:SqlDataSource>
<asp:Button ID="btn_edit" runat="server" Text="Edit" Width="60px"
onclick="btn_edit_Click" />
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
<asp:Label ID="lbl_1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td class="style3">
Reporter Name :</td>
<td>
<asp:TextBox ID="txt_rname" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
</td>
</tr>
<tr>
<td class="style3">
Reporter Email :</td>
<td>
<asp:TextBox ID="txt_remail" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style3">
</td>
<td>
</td>
</tr>
<tr>
<td class="style3">
Reporter Mobile :</td>
<td>
<asp:TextBox ID="txt_rmobile" runat="server" Width="200px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style6">
</td>
<td class="style7">
</td>
</tr>
<tr>
<td class="style6" colspan="2">
<asp:Label ID="lbl_remark" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style6">
</td>
<td class="style7">
</td>
</tr>
<tr>
<td class="style2" colspan="2">
<asp:Button ID="btn_Save" runat="server" style="text-align: center" Text="Update Record"
Width="120px" onclick="btn_Save_Click" />
</td>
</tr>
<tr>
<td class="style4">
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Thanks Pradeep. The following code fixed my problem
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
txt_rname.Enabled = false;
txt_rmobile.Enabled = false;
txt_remail.Enabled = false;
con.Open();
// string qry = "select * from Reporter where Reporter_ID= " + DropDownList1.SelectedValue + "";
lbl_1.Text = Session["id"].ToString();
string qry = "select * from Reporter where Reporter_ID= " + Session["id"] + " ";
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt); // table data-> data table
con.Close();
txt_rname.Text = dt.Rows[0][1].ToString();
txt_remail.Text = dt.Rows[0][2].ToString();
txt_rmobile.Text = dt.Rows[0][3].ToString();
}
else
{
}
}
Related
I have three div which each is shown within a gap of 3 seconds interval. On Page_load it shows first div data, then after 3 seconds, it shows another div data.
So,
now what I want is, Now after more 3 seconds, I want to show the third div data.
Here is my simple HTML with TIMER
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<table style="width: 100%; text-align: center" border="1" runat="server" id="tbl1Details">
<colgroup>
<col width="5%" />
<col width="45%" />
</colgroup>
<tr>
<td colspan="3">
<asp:Label ID="lblpltfrm_Number" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
SR.
</td>
<td>
Stop
</td>
<td>
Time
</td>
</tr>
<tr>
<td id="tTime" runat="server">
<div id="list" runat="server">
</div>
</td>
<td>
<table style="width: 100%">
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<tr>
<td style="text-align: left">
<div id="dv_FromStop" runat="server">
</div>
</td>
<td style="text-align: left">
<div id="Dv_ToStop" runat="server">
</div>
</td>
</tr>
</table>
</td>
<td>
<div id="dv_Time" runat="server">
</div>
</td>
</tr>
</table>
<!--2nd Screen-->
<table style="width: 100%; text-align: center" border="1" runat="server" id="tbl2Details"
visible="false">
<colgroup>
<col width="5%" />
<col width="45%" />
</colgroup>
<tr>
<td colspan="3">
<asp:Label ID="lbl2ndpltfrm" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
SR.
</td>
<td>
Stop
</td>
<td>
Time
</td>
</tr>
<tr>
<td>
<div id="dv2SRNo" runat="server">
</div>
</td>
<td>
<table style="width: 100%">
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<tr>
<td style="text-align: left">
<div id="dv2From_Stop" runat="server">
</div>
</td>
<td style="text-align: left">
<div id="dv2ToStop" runat="server">
</div>
</td>
</tr>
</table>
</td>
<td>
<div id="dv2Time" runat="server">
</div>
</td>
</tr>
</table>
<table id="tbl3Details" style="width: 100%; text-align: center" border="1" runat="server">
<colgroup>
<col width="5%" />
<col width="45%" />
</colgroup>
<tr>
<td colspan="3">
<asp:Label ID="lbl3rdpltfrm" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
SR.
</td>
<td>
Stop
</td>
<td>
Time
</td>
</tr>
<tr>
<td>
<div id="dv3SRNo" runat="server">
</div>
</td>
<td>
<table style="width: 100%">
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<tr>
<td style="text-align: left">
<div id="dv3From_Stop" runat="server">
</div>
</td>
<td style="text-align: left">
<div id="dv3To_Stop" runat="server">
</div>
</td>
</tr>
</table>
</td>
<td>
<div id="dv3Time" runat="server">
</div>
</td>
</tr>
</table>
<asp:Timer ID="Timer1" runat="server" OnTick="GetTime2" Interval="3000" />
</ContentTemplate>
</asp:UpdatePanel>
Now main thing which handles is the code behind of this:-
Have a look
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GetPlt1Details();
Session["NextPlatForm"] = 1;
}
}
protected void GetPlt1Details()
{
lblpltfrm_Number.Text = "PlatForm 1";
list.InnerHtml = "";
dv_FromStop.InnerHtml = "";
Dv_ToStop.InnerHtml = "";
dv_Time.InnerHtml = "";
int svalue = Convert.ToInt32(Session["ReloadValue"]);
DataTable obj_Dt = new DataTable();
OracleConnection obj_Connection = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConn"].ToString());
string Query = "Select x.SR_NO,x.FROM_STOP,x.TO_STOP,x.ORIGIN_STOP_TIME from XXACL_PN_BUS_TIMETABLE x WHERE SCREEN_NUMBER=1";
using (OracleCommand obj_Command = new OracleCommand(Query))
{
OracleDataAdapter obj_Adapter = new OracleDataAdapter(obj_Command);
obj_Command.Connection = obj_Connection;
obj_Adapter.SelectCommand = obj_Command;
obj_Adapter.Fill(obj_Dt);
int Count = obj_Dt.Rows.Count;
for (int i = 0; i < obj_Dt.Rows.Count; i++)
{
if (i < 2)
{
list.InnerHtml = list.InnerHtml + "<br />" +
obj_Dt.Rows[i]["SR_NO"];
dv_FromStop.InnerHtml = dv_FromStop.InnerHtml + "<br />" +
obj_Dt.Rows[i]["FROM_STOP"];
Dv_ToStop.InnerHtml = Dv_ToStop.InnerHtml + "<br />" +
obj_Dt.Rows[i]["TO_STOP"];
dv_Time.InnerHtml = dv_Time.InnerHtml + "<br />" +
obj_Dt.Rows[i]["ORIGIN_STOP_TIME"];
}
else
{
Session["NextPlatForm"] = 1;
Session["A"] = "PlatForm 1";
dv2SRNo.InnerHtml = dv2SRNo.InnerHtml + "<br />" +
obj_Dt.Rows[i]["SR_NO"];
dv2From_Stop.InnerHtml = dv2From_Stop.InnerHtml + "<br />" +
obj_Dt.Rows[i]["FROM_STOP"];
dv2ToStop.InnerHtml = dv2ToStop.InnerHtml + "<br />" +
obj_Dt.Rows[i]["TO_STOP"];
dv2Time.InnerHtml = dv2Time.InnerHtml + "<br />" +
obj_Dt.Rows[i]["ORIGIN_STOP_TIME"];
}
}
}
}
protected void GetTime2(object sender, EventArgs e)
{
int pvalue = Convert.ToInt32(Session["NextPlatForm"]);
if (pvalue != 2)
{
tbl1Details.Visible = false;
tbl2Details.Visible = true;
string a = string.Empty;
a = Convert.ToString(Session["A"]);
if (string.IsNullOrEmpty(a) == false)
{
lbl2ndpltfrm.Text = a;
}
}
else
{
tbl1Details.Visible = true;
tbl2Details.Visible = false;
lblpltfrm_Number.Text = "PlatForm 2";
list.InnerHtml = "";
dv_FromStop.InnerHtml = "";
Dv_ToStop.InnerHtml = "";
dv_Time.InnerHtml = "";
OracleConnection obj_Connection = new OracleConnection(System.Configuration.ConfigurationManager.ConnectionStrings["OracleConn"].ToString());
string Query2 = "Select x.SR_NO,x.FROM_STOP,x.TO_STOP,x.ORIGIN_STOP_TIME from XXACL_PN_BUS_TIMETABLE x WHERE SCREEN_NUMBER=2";
DataTable obj_Dt = new DataTable();
using (OracleCommand obj_Command = new OracleCommand(Query2))
{
OracleDataAdapter obj_Adapter = new OracleDataAdapter(obj_Command);
obj_Command.Connection = obj_Connection;
obj_Adapter.SelectCommand = obj_Command;
obj_Adapter.Fill(obj_Dt);
int Count = obj_Dt.Rows.Count;
for (int i = 0; i < obj_Dt.Rows.Count; i++)
{
list.InnerHtml = list.InnerHtml + "<br />" +
obj_Dt.Rows[i]["SR_NO"];
dv_FromStop.InnerHtml = dv_FromStop.InnerHtml + "<br />" +
obj_Dt.Rows[i]["FROM_STOP"];
Dv_ToStop.InnerHtml = Dv_ToStop.InnerHtml + "<br />" +
obj_Dt.Rows[i]["TO_STOP"];
dv_Time.InnerHtml = dv_Time.InnerHtml + "<br />" +
obj_Dt.Rows[i]["ORIGIN_STOP_TIME"];
}
}
}
Session["NextPlatForm"] = 2;
}
Now, With respect to this, how can I show the third div data
I have 3 dropdown lists:
<tr>
<td align="left" class="style2">
Emp Code</td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel6" runat="server">
<ContentTemplate>
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddcode" runat="server" AutoPostBack="True"
onselectedindexchanged="ddcode_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddcode" ForeColor="Red">*
</asp:RequiredFieldValidator>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="left" class="style2">
Company</td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddcompany" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddcompany" ForeColor="Red">*
</asp:RequiredFieldValidator>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td align="left" class="style2">
Branch</td>
<td align="left">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div class="input-prepend" title="Select Machine ID" data-rel="tooltip">
<asp:DropDownList ID="ddbranch" runat="server"
onselectedindexchanged="ddbranch_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="ddbranch" ForeColor="Red">*
</asp:RequiredFieldValidator>
</ContentTemplate> </asp:UpdatePanel>
</td>
</tr>
I also have a dropdown class:
public class Dropdown
{
Connection con = new Connection();
public void dropdwnlist(string qry, DropDownList ddl)
{
con.gettable(qry);
if (con.dt.Rows.Count > 0)
{
if (con.dt.Columns.Count == 2)
{
string str1 = con.dt.Columns[0].ColumnName.ToString();
string str2 = con.dt.Columns[1].ColumnName.ToString();
ddl.DataValueField = str1;
ddl.DataTextField = str2;
ddl.DataSource = con.dt;
ddl.DataBind();
con.dt.Columns.Remove(str1);
con.dt.Columns.Remove(str2);
}
else
{
string str = con.dt.Columns[0].ColumnName.ToString();
ddl.DataValueField = str;
ddl.DataTextField = str;
ddl.DataSource = con.dt;
ddl.DataBind();
con.dt.Columns.Remove(str);
//con.dt.Columns.Remove(str2);
}
}
ddl.Items.Insert(0, ("--Select--"));
}
My button click is:
Dropdown dwn = new Dropdown();
string str = "";
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillempcode();
//fillcompany();
//filldepartment();
}
}
protected void fillempcode()
{
string str = "select MachID,EmpCode from EmpDetails where StatusID='0'";
dwn.dropdwnlist(str, ddcode);
}
protected void fillcompany()
{
//Where EmpCode='" + ddcode.SelectedItem.Text + "'
str = "select CompanyID,CompanyName from Company ";
dwn.dropdwnlist(str, ddcompany);
str = "select CompanyID from View_Company where EmpCode='" + ddcode.SelectedItem.Text + "'";
dr=conn.query(str);
if (dr.Read())
{
string id = dr[0].ToString();
ddcompany.SelectedItem.Text = id;
}
}
protected void fillbranch()
{
//where EmpCode='" + ddcode.SelectedItem.Text + "'
string str = "select BranchID,BranchName from View_Branch";
dwn.dropdwnlist(str, ddbranch);
}
My problem is I want to enable all dropdownllist when I selected on EmpCode dropdown list. now it works fine but Company and Branch dropdown shows same value twise. may I know the reason?
Try rewriting the dropdwnlist method like this,
public void dropdwnlist(string qry, DropDownList ddl)
{
ddl.Items.Clear();
con.gettable(qry);
if (con.dt.Rows.Count > 0)
{
if (con.dt.Columns.Count == 2)
{
string str1 = con.dt.Columns[0].ColumnName.ToString();
string str2 = con.dt.Columns[1].ColumnName.ToString();
ddl.DataValueField = str1;
ddl.DataTextField = str2;
ddl.DataSource = con.dt;
ddl.DataBind();
con.dt.Columns.Remove(str1);
con.dt.Columns.Remove(str2);
}
else
{
string str = con.dt.Columns[0].ColumnName.ToString();
ddl.DataValueField = str;
ddl.DataTextField = str;
ddl.DataSource = con.dt;
ddl.DataBind();
con.dt.Columns.Remove(str);
//con.dt.Columns.Remove(str2);
}
}
ddl.Items.Insert(0, ("--Select--"));
ddl.SelectedIndex = 0;
}
Hope this helps...
I am using repeater control and showing paging '<< Previous next >>' it is working fine.
But i want to show links (rolling numbers) in between like below:
Previous << 1 2 3 4 5 next >>
have a look this picture for idea.
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" style="border-color: #336699;" cellspacing="0">
<tr style="background-color:#336699; color:White;">
<td>Product ID</td>
<td>Product Name</td>
<td>Unit Price</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ProductID") %></td>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("UnitPrice") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<div> <table width="500px" border="0" cellspacing="5" cellpadding="5">
<tr>
<td width="25%"> <asp:Label ID="Label2" runat="server" />
</td>
<td width="25%"><asp:HyperLink ID="linkPrev" runat="server">« Previous</asp:HyperLink>
</td>
<td width="25%"><asp:HyperLink ID="linkNext" runat="server">Next »</asp:HyperLink></td>
<td width="25%"><asp:Label ID="Label1" runat="server" /> </td>
</tr>
</table> </div>
c#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from Products", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();
con.Close();
DataTable table = new DataTable();
da.Fill(table);
PagedDataSource pds = new PagedDataSource();
pds.DataSource = table.DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
int currentPage;
if (Request.QueryString["page"] != null)
{
currentPage = Int32.Parse(Request.QueryString["page"]);
}
else
{
currentPage = 1;
}
pds.CurrentPageIndex = currentPage - 1;
Label1.Text = "Page " + currentPage + " of " + pds.PageCount;
Label2.Text = "Results" + pds.DataSourceCount;
if (!pds.IsFirstPage)
{
linkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage - 1);
}
if (pds.IsFirstPage)
{
linkPrev.Visible = false;
linkPrev.Style["display"] = "block";
}
if (!pds.IsLastPage)
{
linkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (currentPage + 1);
}
if (pds.IsLastPage)
{
linkNext.Visible = false;
linkPrev.Style["display"] = "block";
}
Repeater1.DataSource = pds;
Repeater1.DataBind();
}
}
I am getting an error("input string was not in a correct format") every time I click the lnkUpdate button. Am i missing something? I can't find what's wrong with the codes in the updating event
aspx
<table class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Received</th>
<th>Remaining</th>
<th>Ordered</th>
<th></th>
</tr>
</thead>
<tbody>
<asp:ListView ID="lvSODetails" runat="server"
onitemcanceling="lvSODetails_ItemCanceling"
onitemediting="lvSODetails_ItemEditing"
onitemupdating="lvSODetails_ItemUpdating" >
<ItemTemplate>
<tr>
<td><%# Eval("ProductID") %></td>
<td><%# Eval("ProductName") %></td>
<td><%# Eval("Received") %></td>
<td><%# Eval("Remaining")%></td>
<td><%# Eval("Quantity") %></td>
<td>
<asp:LinkButton ID="lnkEdit" runat="server"
class="glyphicon glyphicon-pencil" CommandName="Edit" />
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<asp:Panel ID="pnlDetails" runat="server" DefaultButton="lnkUpdate">
<tr>
<td><asp:Label ID="lblID" runat="server" Text='<%# Eval("ProductID") %>' /></td>
<td><%# Eval("ProductName") %></td>
<td><asp:TextBox ID="txtQtyReceived" runat="server" /></td>
<td><asp:Label ID="lblRemaining" runat="server" Text='<%# Eval("Remaining") %>' /></td>
<td><%# Eval("Quantity") %></td>
<td>
<asp:LinkButton ID="lnkUpdate" runat="server"
class="glyphicon glyphicon-ok" CommandName="Update" />
<asp:LinkButton ID="lnkCancel" runat="server"
class="glyphicon glyphicon-remove" CommandName="Cancel" />
</td>
</tr>
</asp:Panel>
</EditItemTemplate>
</asp:ListView>
</tbody>
</table>
c# code
protected void lvSODetails_ItemEditing(object sender, ListViewEditEventArgs e)
{
lvSODetails.EditIndex = e.NewEditIndex;
GetInfo();
GetDetails();
}
protected void lvSODetails_ItemCanceling(object sender, ListViewCancelEventArgs e)
{
lvSODetails.EditIndex = -1;
GetInfo();
GetDetails();
}
protected void lvSODetails_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
string ProdID = (lvSODetails.Items[e.ItemIndex].FindControl("lblID") as Label).Text;
string Received = (lvSODetails.Items[e.ItemIndex].FindControl("txtQtyReceived") as TextBox).Text;
string Remaining = (lvSODetails.Items[e.ItemIndex].FindControl("lblRemaining") as Label).Text;
if (int.Parse(Received) < 0) // <~ this is where the code stops (error"input string was not in a correct format")
{
error.Visible = true;
lblError.Text = "Items received must not be lower than zero";
}
else if (int.Parse(Received) >= 0 && int.Parse(Received) <= int.Parse(Remaining))
{
SODetails = (DataTable)Session["sodelivery"];
foreach (DataRow row in SODetails.Rows)
{
if (row["ProductID"].ToString() == ProdID)
{
row["Received"] = Received;
break;
}
}
}
lvSODetails.EditIndex = -1;
GetInfo();
GetDetails();
}
~~~~ Edit ~~~~
listview databinding code
void GetDetails()
{
if (SODetails == null)
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT sod.ProductID, p.ProductName, sod.SOQtyReceived AS Received, " +
"(sod.SOQtyOrdered - sod.SOQtyReceived) AS Remaining, sod.SOQtyOrdered AS Quantity FROM SODetails AS sod " +
"INNER JOIN Products AS p ON sod.ProductID=p.ProductID WHERE sod.SONo=#SONo";
cmd.Parameters.Add("#SONo", SqlDbType.Int).Value = Request.QueryString["ID"].ToString();
SqlDataAdapter data = new SqlDataAdapter(cmd);
SODetails = new DataTable();
data.Fill(SODetails);
DataRow[] rowList = SODetails.Select();
foreach (DataRow dr in rowList)
{
dr["Received"] = "0";
}
lvSODetails.DataSource = SODetails;
lvSODetails.DataBind();
con.Close();
Session["sodelivery"] = SODetails;
}
else
{
lvSODetails.DataSource = SODetails;
lvSODetails.DataBind();
Session["sodelivery"] = SODetails;
}
}
Your problem isn't that you can't access the TextBox.
Use int.TryParse instead of int.Parse since you are reading user input.
int received;
if (!int.TryParse(Received, out received) || received < 0)
{
error.Visible = true;
lblError.Text = "Items received must not be lower than zero";
}
I want to assign same datasource to multiple DropDownLists in a form, when the page loads, only the first drop down list has filled the content of datasource, others are empty, what's the missing point? thanks for answers. Here are the codes;
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="panel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="div1" align="center">
<table>
<tr>
<td><b>Brand-Model</b></td>
<td><asp:TextBox ID="brandText" runat="server" BorderStyle="Inset"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="brandText" Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td><b>Black</b></td>
<td><asp:DropDownList ID="blackList" runat="server"></asp:DropDownList></td>
<td><asp:HyperLink ID="HyperLink1" runat="server" Text="Add Cartridge" NavigateUrl="~/Admin/addCartridge.aspx"></asp:HyperLink></td>
</tr>
<tr>
<td><b>Color1</b></td>
<td><asp:DropDownList ID="colorList1" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Color2</b></td>
<td><asp:DropDownList ID="colorList2" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Color3</b></td>
<td><asp:DropDownList ID="colorList3" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other1</b></td>
<td><asp:DropDownList ID="otherColor1" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other2</b></td>
<td><asp:DropDownList ID="otherColor2" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td><b>Other3</b></td>
<td><asp:DropDownList ID="otherColor3" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="submit" runat="server" Text="Submit" OnClick="submit_OnClick" /></td>
</tr>
<tr>
<td></td>
<td><asp:Label ID="submitInfo" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="submit" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</form>
protected void FillTheDropDownLists()
{
SqlApplication con = new SqlApplication();
try
{
SqlCommand cmd = new SqlCommand("SELECT name FROM BT.dbo.Cartridge ORDER BY name", con.GetConnection());
con.OpenSqlConnection();
SqlDataReader reader = cmd.ExecuteReader();
blackList.DataValueField = "name";
blackList.DataSource = reader;
blackList.DataBind();
colorList1.DataValueField = "name";
colorList1.DataSource = reader;
colorList1.DataBind();
colorList2.DataValueField = "name";
colorList2.DataSource = reader;
colorList2.DataBind();
colorList3.DataValueField = "name";
colorList3.DataSource = reader;
colorList3.DataBind();
otherColor1.DataValueField = "name";
otherColor1.DataSource = reader;
otherColor1.DataBind();
otherColor2.DataValueField = "name";
otherColor2.DataSource = reader;
otherColor2.DataBind();
otherColor3.DataValueField = "name";
otherColor3.DataSource = reader;
otherColor3.DataBind();
reader.Close();
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine("Exception: " + err.Message);
}
finally
{
con.CloseSqlConnection();
}
}
Its becasue you are using dataReader instead of this use datatable will work for you. Reader is readonly and forward only thats why only first dropdonw get filled with data and others are empty.
changed code :
protected void FillTheDropDownLists()
{
SqlApplication con = new SqlApplication();
try
{
SqlCommand cmd = new SqlCommand("SELECT name FROM BT.dbo.Cartridge ORDER BY name", con.GetConnection());
con.OpenSqlConnection();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader );
blackList.DataValueField = "name";
blackList.DataSource = dt ;
blackList.DataBind();
colorList1.DataValueField = "name";
colorList1.DataSource = dt ;
colorList1.DataBind();
colorList2.DataValueField = "name";
colorList2.DataSource = reader;
colorList2.DataBind();
colorList3.DataValueField = "name";
colorList3.DataSource = dt ;
colorList3.DataBind();
otherColor1.DataValueField = "name";
otherColor1.DataSource = dt ;
otherColor1.DataBind();
otherColor2.DataValueField = "name";
otherColor2.DataSource = dt ;
otherColor2.DataBind();
otherColor3.DataValueField = "name";
otherColor3.DataSource = dt ;
otherColor3.DataBind();
reader.Close();
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine("Exception: " + err.Message);
}
finally
{
con.CloseSqlConnection();
}
}
According to the documentation data reader is forward only. Use data table or read into a list and bind your combos to it.
for (int i = 1; i <= 260; i++)
{
ContentPlaceHolder maincontent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
DropDownList a = (DropDownList)maincontent.FindControl("ddl" + i);
a.DataSource = ds;
a.DataTextField = "Options";
a.DataValueField = "id";
a.DataBind();
a.Items.Insert(0, new ListItem("Select", "0", true));
}