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?

Thanks,
Rohith.

>the database is not getting updated with the entered value.

Maybe that particular code/event is not fired. Show us your code please.

>the database is not getting updated with the entered value.

Maybe that particular code/event is not fired. Show us your code please.

Hi,
here is the code used to edit the fields.

<EditItemTemplate>
       <table width="100%" border="0" cellpadding="1" cellspacing="1">  
       <td>
       <tr>   <td>
           <asp:Label ID="lbEmp" runat="server" Text="EmpId"></asp:Label>  <asp:TextBox ID="a" runat="server" 
                 Text='<%# Bind("EmpId") %>'  
                 ForeColor="Blue" BorderStyle="none" readonly="true" 
                 BorderWidth="0px" >
    </asp:TextBox></td>
        <td>
       
           <asp:Label ID="lbName" runat="server" Text="EmpName"></asp:Label><asp:TextBox ID="b" runat="server" 
                AutoPostBack="false" Text='<%#Bind("EmpName") %>'  
                 ForeColor="Blue" BorderStyle="none"  
                 BorderWidth="0px" ></asp:TextBox></td>
       <td>
           <asp:Label ID="lbSal" runat="server" Text="EmpSalary"></asp:Label><asp:TextBox ID="c" runat="server" 
                AutoPostBack="false" Text='<%#Bind("EmpSalary") %>'  
                 ForeColor="Blue" BorderStyle="none"
                 BorderWidth="0px" ></asp:TextBox></td></tr></td>
   </table>   <asp:linkButton ID="update"  runat="server" 
             Text="Update"  
                    style="height: 26px" CommandName="Update" />
                    <asp:LinkButton Text="Cancel" CommandName="Cancel" 
                      Runat="server" ID="cancel" />
   </EditItemTemplate>

Code behind:

protected void btnUpdate_Click(object sender, System.Web.UI.WebControls.DataListCommandEventArgs e) 
    {
        
        StringBuilder strSql = new StringBuilder(string.Empty);
        int yourValue = Convert.ToInt32(Request.QueryString["id"]);
        SqlConnection myConnection = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        TextBox empnme = new TextBox();
        TextBox empsal = new TextBox();
        myConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csRohith1"].ConnectionString;
        foreach(DataListItem data in databilling.Items)
        {
            
            empnme = (TextBox)data.FindControl("b");
             empsal = (TextBox)data.FindControl("c");
             string empname = empnme.Text;
            string  empsalary = empsal.Text;
             string strUpdate =
                        "Update Emp set EmpName = '" + empname + "'," + " EmpSalary = '" + empsalary + "'" +
                        " WHERE EmpId ='" + yourValue + "'" + ";";
             strSql.Append(strUpdate);
        }
        
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strSql.ToString();
        cmd.Connection = myConnection;
        myConnection.Open();

        cmd.ExecuteReader();
        
        myConnection.Close();
        databilling.EditItemIndex = -1;
        load();
       
        

    }

Thanks,
Rohith

Try this and report here,

protected void btnUpdate_Click(object sender, System.Web.UI.WebControls.DataListCommandEventArgs e) 
    {
        TextBox a=(TextBox)e.Item.FindControl("a"); 
        TextBox b=(TextBox)e.Item.FindControl("b");
        TextBox c=(TextBox)e.Item.FindControl("c");
        Response.Write(a.Text + " " + b.Text  +  " " + c.Text); 
     }

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?

Thanks,
Rohith.

Refer this link

http://sureshsharmaaspdotnet.wordpress.com/2008/05/15/editupdate-and-delete-in-datalist/

http://programming.top54u.com/post/ASP-Net-DataList-Update-command-using-C-sharp.aspx

Hi,
I tried the above code which you have suggested. It still printing the old value of the text box. Can you please suggest any change to my code.
Regards,
Rohith

Hello,
I have even checked the links, but the code is not working. Actually, the changed value of the text box in not fetched. It would be my pleasure if someone helps in this regard.

Check this code for your reference,

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>Datalist Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DataList ID="DataList1" runat="server" 
            oneditcommand="DataList1_EditCommand" onupdatecommand="DataList1_UpdateCommand">
        <ItemTemplate>
         <p>No</p>
         <p>
            <asp:Label ID="l1" runat="server" Text='<%# Eval("No") %>'></asp:Label>
         </p>
         <p>Name</p>
         <p>
            <asp:Label ID="l2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
         </p>
         <p>
            <asp:Button ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" />
         </p>
        </ItemTemplate>
        
        <EditItemTemplate>
         <ItemTemplate>
         <p>No</p>
         <p>
           <asp:TextBox ID="eno" runat="server" Text='<%# Eval("No") %>'></asp:TextBox>
         </p>
         <p>Name</p>
         <p>
           <asp:TextBox ID="ename" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox> 
         </p>
         <p>
            <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Edit" />
         </p>
        </EditItemTemplate>
        </asp:DataList>
        </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Show();
        }
    }

    void Show()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("No");
        dt.Columns.Add("Name");
        dt.Rows.Add("1", "A");
        dt.Rows.Add("2", "B");

        DataList1.DataSource = dt;
        DataList1.DataBind();
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        Show();
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        TextBox tx1 =(TextBox) e.Item.FindControl("eno");
        TextBox tx2 = (TextBox)e.Item.FindControl("ename");

        Response.Write(tx1.Text + " " + tx2.Text);
    }
}

Check this code for your reference,

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>Datalist Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DataList ID="DataList1" runat="server" 
            oneditcommand="DataList1_EditCommand" onupdatecommand="DataList1_UpdateCommand">
        <ItemTemplate>
         <p>No</p>
         <p>
            <asp:Label ID="l1" runat="server" Text='<%# Eval("No") %>'></asp:Label>
         </p>
         <p>Name</p>
         <p>
            <asp:Label ID="l2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
         </p>
         <p>
            <asp:Button ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" />
         </p>
        </ItemTemplate>
        
        <EditItemTemplate>
         <ItemTemplate>
         <p>No</p>
         <p>
           <asp:TextBox ID="eno" runat="server" Text='<%# Eval("No") %>'></asp:TextBox>
         </p>
         <p>Name</p>
         <p>
           <asp:TextBox ID="ename" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox> 
         </p>
         <p>
            <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Edit" />
         </p>
        </EditItemTemplate>
        </asp:DataList>
        </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Show();
        }
    }

    void Show()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("No");
        dt.Columns.Add("Name");
        dt.Rows.Add("1", "A");
        dt.Rows.Add("2", "B");

        DataList1.DataSource = dt;
        DataList1.DataBind();
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        Show();
    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        TextBox tx1 =(TextBox) e.Item.FindControl("eno");
        TextBox tx2 = (TextBox)e.Item.FindControl("ename");

        Response.Write(tx1.Text + " " + tx2.Text);
    }
}

Hi,
I have tried the same code but it is not working. Problem is the altered value in the textbox is not getting reflected.
Thanks,
Rohith

Please post complete code. You may also use attachment (.zip) feature.

Please post complete code. You may also use attachment (.zip) feature.

Hi,
I am attaching the complete code. Please look at it and suggest me the changes.

<asp:DataList ID="dataenv" runat="server" CellPadding="4" 
                    ForeColor="#333333" RepeatColumns="2"  RepeatLayout="Flow" 
                   OnUpdateCommand ="btnUpdate_Click" OnEditCommand="Edit_Click" OnCancelCommand="Cancel_Click">
                    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                    <AlternatingItemStyle BackColor="White" />
                    <ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />
                    <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                     <ItemTemplate>
                         &nbsp;
       <table width="100%" border="0" cellpadding="1" cellspacing="1">   
       <tr>   <td>
           <asp:Label ID="lbEmp1" runat="server" Text="EmpId"></asp:Label>  
                 &#39;<%# DataBinder.Eval(Container.DataItem,"EmpId") %>&#39;  
                
    </td>
        <td>
       
            <br />
       
           <asp:Label ID="lbName1" runat="server" Text="EmpName"></asp:Label>
              <asp:TextBox ID="txtname" runat="server" Text=  '<%#Eval("EmpName") %>'  ></asp:TextBox>
                 </td>
       <td>
           <asp:Label ID="lbSal1" runat="server" Text="EmpSalary"></asp:Label>
                <asp:TextBox ID="txtsal" runat="server" Text='<%#Eval("EmpSalary") %>' ></asp:TextBox> 
                 </td></tr>
   </table> <asp:LinkButton Text="Edit" CommandName="Edit" 
                      Runat="server" ID="edit" /> 
   </ItemTemplate>
   <EditItemTemplate>
       <table width="100%" border="0" cellpadding="1" cellspacing="1">   
       <tr>   <td>
           <asp:Label ID="lbEmp" runat="server" Text="EmpId"></asp:Label>  <asp:TextBox ID="a" runat="server" 
                 Text='<%# Eval("EmpId") %>'  
                 ForeColor="Blue" BorderStyle="none" readonly="true"
                 BorderWidth="0px" >
    </asp:TextBox></td>
        <td>
       
           <asp:Label ID="lbName" runat="server" Text="EmpName"></asp:Label><asp:TextBox ID="b" runat="server" 
                Text='<%#Eval("EmpName") %>'  
                 ForeColor="Blue" BorderStyle="none" 
                 BorderWidth="0px" ></asp:TextBox></td>
       <td>
           <asp:Label ID="lbSal" runat="server" Text="EmpSalary"></asp:Label><asp:TextBox ID="c" runat="server" 
                Text='<%#Eval("EmpSalary") %>'  
                 ForeColor="Blue" BorderStyle="none" 
                 BorderWidth="0px" ></asp:TextBox></td></tr>
   </table>   <asp:linkButton ID="update"  runat="server" 
             Text="Update"  
                    style="height: 26px" CommandName="Update" />
                    <asp:LinkButton Text="Cancel" CommandName="Cancel" 
                      Runat="server" ID="cancel" />
   </EditItemTemplate>
   
                </asp:DataList>

code behind:

protected void btnUpdate_Click(object sender, DataListCommandEventArgs e)
    {
        int yourValue = Convert.ToInt32(Request.QueryString["id"]);
        //Create sql connection and command
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csRohith1"].ConnectionString;
        string empname = ((TextBox)e.Item.FindControl("b")).Text;
        string empsalary = ((TextBox)e.Item.FindControl("c")).Text;
        string strUpdate =
                        "Update Emp set EmpName = '" + empname + "'," + " EmpSalary = '" + empsalary + "'" +
                        " WHERE EmpId ='" + yourValue + "'" + ";";

        SqlCommand cmd = new SqlCommand(strUpdate, myConnection);

        myConnection.Open();
        cmd.ExecuteNonQuery();

        myConnection.Close();
        dataenv.EditItemIndex = -1;
        load();



    }
    protected void Edit_Click(object sender, DataListCommandEventArgs e)
    {
        dataenv.EditItemIndex = e.Item.ItemIndex;
        load();
    }
    protected void Cancel_Click(object sender, DataListCommandEventArgs e)
    {
        dataenv.EditItemIndex = -1;
        load();
    }
    private void load()
    {
        int yourValue = Convert.ToInt32(Request.QueryString["id"]);
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csRohith1"].ConnectionString;
        myConnection.Open();
        SqlCommand cmd = new SqlCommand("Select EmpId,EmpName,EmpSalary from Emp where EmpId=" + yourValue, myConnection);
        dataenv.DataSource = cmd.ExecuteReader();

        dataenv.DataBind();
        myConnection.Close();
    }

I think querystring might be a problem.

protected void btnUpdate_Click(object sender, DataListCommandEventArgs e)
    {
         string empid = ((TextBox)e.Item.FindControl("a")).Text;
        //Create sql connection and command
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csRohith1"].ConnectionString;
          
        string empname = ((TextBox)e.Item.FindControl("b")).Text;
        string empsalary = ((TextBox)e.Item.FindControl("c")).Text;
        string strUpdate =
                        "Update Emp set EmpName = '" + empname + "'," + " EmpSalary = '" + empsalary + "'" +
                        " WHERE EmpId ='" + empid + "'";

        SqlCommand cmd = new SqlCommand(strUpdate, myConnection);

        myConnection.Open();
        cmd.ExecuteNonQuery();
        myConnection.Close();
        dataenv.EditItemIndex = -1;
        load();



    }
    protected void Edit_Click(object sender, DataListCommandEventArgs e)
    {
        dataenv.EditItemIndex = e.Item.ItemIndex;
        load();
    }
    protected void Cancel_Click(object sender, DataListCommandEventArgs e)
    {
        dataenv.EditItemIndex = -1;
        load();
    }
    private void load()
    {
        int yourValue = Convert.ToInt32(Request.QueryString["id"]);
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csRohith1"].ConnectionString;
        myConnection.Open();
        SqlCommand cmd = new SqlCommand("Select EmpId,EmpName,EmpSalary from Emp", myConnection);
        dataenv.DataSource = cmd.ExecuteReader();

        dataenv.DataBind();
        myConnection.Close();
    }

I think querystring might be a problem.

protected void btnUpdate_Click(object sender, DataListCommandEventArgs e)
    {
         string empid = ((TextBox)e.Item.FindControl("a")).Text;
        //Create sql connection and command
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csRohith1"].ConnectionString;
          
        string empname = ((TextBox)e.Item.FindControl("b")).Text;
        string empsalary = ((TextBox)e.Item.FindControl("c")).Text;
        string strUpdate =
                        "Update Emp set EmpName = '" + empname + "'," + " EmpSalary = '" + empsalary + "'" +
                        " WHERE EmpId ='" + empid + "'";

        SqlCommand cmd = new SqlCommand(strUpdate, myConnection);

        myConnection.Open();
        cmd.ExecuteNonQuery();
        myConnection.Close();
        dataenv.EditItemIndex = -1;
        load();



    }
    protected void Edit_Click(object sender, DataListCommandEventArgs e)
    {
        dataenv.EditItemIndex = e.Item.ItemIndex;
        load();
    }
    protected void Cancel_Click(object sender, DataListCommandEventArgs e)
    {
        dataenv.EditItemIndex = -1;
        load();
    }
    private void load()
    {
        int yourValue = Convert.ToInt32(Request.QueryString["id"]);
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csRohith1"].ConnectionString;
        myConnection.Open();
        SqlCommand cmd = new SqlCommand("Select EmpId,EmpName,EmpSalary from Emp", myConnection);
        dataenv.DataSource = cmd.ExecuteReader();

        dataenv.DataBind();
        myConnection.Close();
    }

I have even tried this. The problem is i could not get the new values entered in the text boxes. empname and empsalary are storing the old values not the new text which is entered. Is there any way to trigger the textchanged event.

Cap I see the code of page_load event? Please post complete source code. You may use attachment facility here.

Cap I see the code of page_load event? Please post complete source code. You may use attachment facility here.

Thanks a lot. I have rectified the problem. Now every thing is working fine.
Thanks,
Rohith

Please mark this thread as solved. Can I ask what was that problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.