This is a discussion on How to Select a record using Listbox and edit/update it's fields using textboxes? within the ASP and ASP.NET Programming forums, part of the Web Development category; How to Select a record using Listbox and edit/update it's fields using textboxes?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| How to Select a record using Listbox and edit/update it's fields using textboxes? |
| Sponsored Links |
| |||
| Default.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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> </head> <body> <form id="form1" runat="server"> <div> <TABLE class="cdb-AltRow2" id="Table1" style="Z-INDEX: 101; LEFT: 189px; POSITION: absolute; TOP: 20px" cellSpacing="1" cellPadding="1" width="300" border="0"> <TR> <TD>ProductID</TD> <TD><asp:textbox id="txtProductId" runat="server" Enabled="False"></asp:textbox></TD> </TR> <TR> <TD>Qunatity per unit</TD> <TD><asp:textbox id="txtQuantity" runat="server"></asp:textbox></TD> </TR> <TR> <TD>UnitPrice</TD> <TD><asp:textbox id="txtUnitPrice" runat="server"></asp:textbox></TD> </TR> <TR> <TD></TD> <TD><asp:button id="btnUpdate" runat="server" Text="Update"></asp:button></TD> </TR> <TR> <TD></TD> <TD><asp:label id="lblError" runat="server" Visible="False"></asp:label></TD> </TR> </TABLE> <asp:listbox id="ListBox1" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 12px" runat="server" AutoPostBack="True"></asp:listbox> </div> </form> </body> </html> Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { string productid; string strSQL; string strConn; SqlDataReader dr,dr1; SqlConnection mycn; SqlCommand mycmd; private void Page_Load(object sender, System.EventArgs e) { strConn ="server=localhost;uid=sa;database=northwind;pwd=; "; lblError.Visible =false; // Put user code to initialize the page here if (!Page.IsPostBack ) { mycn = new SqlConnection(strConn); strSQL ="Select * from Products"; mycmd = new SqlCommand (strSQL, mycn); mycn.Open (); dr=mycmd.ExecuteReader(CommandBehavior.CloseConnec tion ); ListBox1.DataSource = dr; ListBox1.DataTextField ="ProductID"; ListBox1.DataValueField ="ProductID"; ListBox1.DataBind (); } } private void btnUpdate_Click(object sender, System.EventArgs e) { try { lblError.Visible =false; string updateCmd = "UPDATE Products SET QuantityPerUnit = @QuantityPerUnit ," + "UnitPrice = @UnitPrice where ProductId=@ProductId"; SqlConnection cn = new SqlConnection (strConn); SqlCommand myCommand = new SqlCommand(updateCmd, cn); myCommand.Parameters.Add(new SqlParameter("@QuantityPerUnit", txtQuantity.Text )); myCommand.Parameters.Add(new SqlParameter("@UnitPrice", Convert.ToDouble( txtUnitPrice.Text ))); myCommand.Parameters.Add(new SqlParameter("@ProductId", Convert.ToInt16 ( txtProductId.Text))); cn.Open (); myCommand.ExecuteNonQuery (); lblError.Visible =true; lblError.Text ="Record Updated successfully"; } catch(Exception ex) { lblError.Visible =true; lblError.Text =(ex.Message ); } } private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e) { productid = ListBox1.SelectedItem.Value ; txtProductId.Text =productid.ToString (); strSQL = "Select * from Products where productid =" + productid ; mycn = new SqlConnection(strConn); mycmd = new SqlCommand (strSQL, mycn); mycn.Open (); dr1=mycmd.ExecuteReader(CommandBehavior.CloseConne ction ); if(dr1.Read() ) { txtProductId.Text = dr1["ProductId"].ToString (); txtQuantity.Text = dr1["QuantityPerUnit"].ToString (); txtUnitPrice.Text = dr1["UnitPrice"].ToString (); } else { Response.Write ("No data found"); } } } Last edited by GDevakii : 12-19-2007 at 09:32 PM. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Extracting specific elements from listbox | Kirubhananth | ASP and ASP.NET Programming | 5 | 04-08-2008 10:35 PM |
| How i will get the selected value from Listbox( if selection mode=multiple) | Mramesh | C# Programming | 3 | 02-07-2008 04:20 AM |
| ASP.net threading issue when populating a listbox | $enthil | ASP and ASP.NET Programming | 6 | 11-04-2007 11:24 PM |
| Listbox onselected event problem | $enthil | ASP and ASP.NET Programming | 2 | 11-01-2007 10:36 PM |
| How to clear all the textboxes in my form in ASP DOT NET and C#? | a.deeban | ASP and ASP.NET Programming | 3 | 09-20-2007 07:23 AM |