Click here to Skip to main content
14,869,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have successfully Insert the Dynamic TextBox Values from GridView to Database.
But Now I want to Update the Dynamic TextBox Values from GridView to Database.

There is some "little error" in my code.

So Please tell me .

Here is my code-

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Specialized;
using System.Text;

C#
public partial class SiteAttendence : System.Web.UI.Page
{
    string connectionString = WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {

    }


C#
private void UpdateRecords(StringCollection sc)
    {
        int rowIndex = 0;
        SqlConnection conn = new SqlConnection(connectionString);
        StringBuilder sb = new StringBuilder(string.Empty);
        string[] splitItems = null;
        foreach (string item in sc)
        {
            TextBox box3 = (TextBox)GridView2.Rows[rowIndex].Cells[3].FindControl("TextBox3");
            TextBox box4 = (TextBox)GridView2.Rows[rowIndex].Cells[4].FindControl("TextBox4");

             string sqlStatement = "update SampleTable set Intime2='" + box3.Text + "',Outtime2='" + box4.Text + "' where ID='" + GridView2.Rows[rowIndex]+ "'";
            if (item.Contains(","))
            {
                splitItems = item.Split(",".ToCharArray());
                sb.AppendFormat("{0}('{1}','{2}'); ", sqlStatement, splitItems[0], splitItems[1]);

            }

        }

        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();


            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
           // string msg = "Insert Error:";
           // msg += ex.Message;
           // throw new Exception(msg);

        }
        finally
        {
            conn.Close();
        }
    }
protected void btnUpdate1_Click(object sender, EventArgs e)
    {
        int rowIndex = 0;
        StringCollection sc = new StringCollection();


        int count = GridView2.Rows.Count;
        for (int i = 0; i < count; i++)
        {
            
            TextBox box3 = (TextBox)GridView2.Rows[rowIndex].Cells[3].FindControl("TextBox3");
            TextBox box4 = (TextBox)GridView2.Rows[rowIndex].Cells[4].FindControl("TextBox4");
            //get the values from the TextBoxes
            //then add it to the collections with a comma "," as the delimited values
            sc.Add( box3.Text + "," + box4.Text);
            rowIndex++;
        }
        //Call the method for executing inserts
        UpdateRecords(sc);
    }
}
Posted
Updated 14-Dec-11 23:56pm
v4

by using GridView1_RowUpdating you can update
   
private void UpdateRecords(StringCollection sc)
{
     SqlConnection conn = new SqlConnection(connectionString);
     
     TextBox box3 = (TextBox)GridView2.Rows[rowIndex].Cells[3].FindControl("TextBox3");
     TextBox box4 = (TextBox)GridView2.Rows[rowIndex].Cells[4].FindControl("TextBox4");
 
     string sqlStatement = "UPDATE SampleTable SET Intime2='" + box3.Text +"', Outtime2='"+ box4.Text +"' WHERE "; 
     //here you must set a condition of where you want your update to be effected!!
      
     try
     {
         conn.Open();
         SqlCommand cmd = new SqlCommand(sqlStatement, conn);
         cmd.CommandType = CommandType.Text;
         cmd.ExecuteNonQuery();
 
         Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
 
     }
     catch (System.Data.SqlClient.SqlException ex)
     {
         // string msg = "Insert Error:";
         // msg += ex.Message;
         // throw new Exception(msg);
     }
     finally
     {
         conn.Close();
     }
}


Also, you might want to set the SQL parameters by passing them using the SQLParameters for more security.
   
v2

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