Hi
I have created dynamic textboxes inside gridview
While adding new row,already existing records are stored in viewstate
and fetch the data from viewstate in to gridview.Also a new row is created.
ID,Column1,Column2,Column3 are database fields
After adding new row,ID value should be incremented.
but ID value is not incremented.This ID value is got from database max(ID).I have given code below.
How to make ID value to be incremented .for every new row is added , Id should be incremented
private void AddNewRowToGrid()
{
SqlConnection conn = new SqlConnection(GetConnectionString());
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT max(ID) from SampleTable",conn);
identity = Convert.ToInt32(cmd.ExecuteScalar());
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox4");
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
dtCurrentTable.Rows[i - 1]["ID"] = box4.Text;
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
rowIndex++;
}
drCurrentRow = dtCurrentTable.NewRow();
int identity1 = identity + 1;//This value is not incremented when new row is added
drCurrentRow["ID"] = identity1;
drCurrentRow["Column1"] = "";
drCurrentRow["Column2"] = "sdfsfd";
drCurrentRow["Column3"] = "sdfsf";
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}Thanks and Regards
N.Ram