David Turner David Turner - 1 year ago 79
ASP.NET (C#) Question

Adding data to database from grid view

I am having some trouble picking up IDs from my grid view in order to add them into my database. I am getting a does not exist error in my.cs file when the ID exists in the grid view it seem to be not able to pick it up.


GridView1.Visible = true;
string IDdata = Session["userID"].ToString();
string OwnerData = txtprojectOwner.Text;
string MasterData = txtScrumMaster.Text;
string devData = userName.Text;

Heres the code I'm having trouble with in my .cs file the userName.Text isn't being picked up and its throwing the error

<asp:ControlParameter ControlID="userName" Name="userName"PropertyName="Text" Type="String" />

Heres the code from inside the list view from the item I'm trying to add into the database. Any help with tis would be appreciated.

Answer Source

You cannot access control like that if your control is in Grid View .

You have to find control from Grid view and after you can access it and you can

get text value of that control .

In Button click event, you can find each rows textbox value and you

can insert into database .

   foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
    TextBox TextBoxfname = row.FindControl("TextBox1") as TextBox ;
    string txtname= TextBoxfname.Text ;// You can insert txtname into database .


GridView1 : ID of gridview

TextBox1 : ID of Textbox

Let me know , if you will be stuck .

Thanks .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download