Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to insert a dynamically created gridview data into database.

Please help me friends.
Posted
Updated 4-Jul-11 20:53pm
v2
Comments
Dalek Dave 5-Jul-11 2:53am    
Edited for Clarity.

Hello,

take a blank row and bind the row as datasource. by this you will get the one blank row in grid afterwords you can add data to them. but yes for this you need have knowledge of view state.


hoping it will help you.. right comments if you need any furthur help

thanks

sanjeev
 
Share this answer
 
Comments
nithibs 5-Jul-11 2:31am    
hi..
i bind the datas already but now i want to insert the grid rows into database....
here i am giving the code for dynamically created grid datas...

protected void addprobtn_Click(object sender, EventArgs e)
{
if (addprobtn.Text == "Add")
{
if ((txtpid.Text.Length > 0) && (txtpname.Text.Length > 0))
{
string pid = txtpid.Text.Trim();
string pname = txtpname.Text.Trim();
AddDataToTable(pid, pname, (DataTable)Session["myDatatable"]);
addpro_Grid2.Visible = true;
addpro_Grid2.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
addpro_Grid2.DataBind();

}
}

else
{
lblmsg.Visible = true;
lblmsg.Text = "Enter Product ID & Product Name";
}
}

private void AddDataToTable(string pid, string pname, DataTable myTable)
{
DataRow row;
row = myTable.NewRow();
row["pid"] = pid;
row["pname"] = pname;
myTable.Rows.Add(row);
}

private DataTable BuildTable()
{
DataTable myDataTable = new DataTable();
myDataTable.Columns.Add("pid").DataType = Type.GetType("System.String");
myDataTable.Columns.Add("pname").DataType = Type.GetType("System.String");
return myDataTable;
}

if i use this above code and enter the data in textboxes and click the add button it will dynamically create the gridview....

now, i want to know how to insert that into database
Christian Graus 5-Jul-11 2:58am    
You can add anything you like in the DB, it's all the same. What you can't do, is use dynamically created stuff that was added in an event, as it's added too late to have viewstate. Just like I said.
The fact it's dynamically created means you won't be able to find it's data unless you insert it before page_load, otherwise, it's viewstate is lost. Once you can get it's data, you insert it like any other.
 
Share this answer
 
Hello,

Now you know your Gridview and their respective rows right. take a button and on click event initialilze the connection for DB. Using a loop create a string that has insertion data from grid and finaly execute that command.

it will insert the data into respecetive DB.

for example point google for How to insert data from gridview to DB. it will give you lots of link.

thanks
Sanjeev
 
Share this answer
 

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