Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am currently using a datalist to update the database with a textbox control.
The problem is that when I am pressing "Edit" and entering values in the textbox and when trying to update the database, the database is not getting updated with the entered value.

Could someone please let me know the exact procedure to update the database with the value from the textbox?
ASP.NET
<asp:DataList ID="DataList1" runat="server"  DataKeyField="memberID"  
       oneditcommand="DataList1_EditCommand" 
        onupdatecommand="DataList1_UpdateCommand">



C#
private void   Load(){
         dal db = new dal();
        DataTable dt = new DataTable();
        dt = db.run_dr("SELECT [memberID], [Name], [Family], [Tell], [Email]);
        DataList1.DataSource = dt;
        DataList1.DataBind();

C#
 protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
       TextBox txtName = (TextBox)e.Item.FindControl("TextName");
       TextBox txtFamily = (TextBox)e.Item.FindControl("TextFamily");
       TextBox txtTell = (TextBox)e.Item.FindControl("TextTell");

if (Page.IsValid)
        {
            db.run_dr("update members set Name=N'" + txtName.Text +
                                          "',Family=N'" + txtFamily.Text +
                                          "',Tell='" + txtTell.Text.Trim() +
                                          "' where  memberID="+                        DataList1.DataKeys[e.Item.ItemIndex].ToString());

            DataList1.DataBind();
            DataList1.EditItemIndex = -1;
        }
    }


Problem is the altered value in the textbox is not getting reflected.

C#
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
   {

       DataList1.EditItemIndex = e.Item.ItemIndex;
       Load();
   }
Posted
Updated 20-Sep-12 20:19pm
v2

I am not sure why it is happening, but I found some of the bugs in your code. May be the values are not getting inserted because of that. Try replacing your command with this:
C#
db.run_dr("update members set Name='" + txtName.Text +
                                          "',Family='" + txtFamily.Text +
                                          "',Tell='" + txtTell.Text.Trim() +
                                          "' where  memberID='"+                        DataList1.DataKeys[e.Item.ItemIndex].ToString()+"'");


Hope it helps.
--Amit
 
Share this answer
 
Comments
jiji2663 22-Sep-12 10:27am    
i use brakpiont to test value of textbox

TextBox txtName = (TextBox)e.Item.FindControl("TextName");
TextBox txtFamily = (TextBox)e.Item.FindControl("TextFamily");
TextBox txtTell = (TextBox)e.Item.FindControl("TextTell");

and,the textbox have old value
Have you tried to put an actual ID to test if DataList1.DataKeys[e.Item.ItemIndex].ToString() does really gives you the ID on your where clause? and try to put this DataList1.EditItemIndex = -1; before DataList1.DataBind();
 
Share this answer
 
Comments
jiji2663 22-Sep-12 10:24am    
yes,it's work
DataList1.DataKeys[e.Item.ItemIndex].ToString()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900