Hi all; I have a error messasge as below: System.NullReferenceException: Object reference not set to an instance of an object.,I don't know what actually happen. I just want to get the value of the specific Id parameter "Edit_PartNo" in the "DataGrid1" String StrSave = "Update Db set PartNo=? where ReportNo='"+ReportID+"'"; OleDbCommand myCommand = new OleDbCommand(StrSave, myConnection); myCommand.Parameters.Add(new OleDbParameter("@EditPartNo", OleDbType.VarChar, 50)); myCommand.Parameters["@EditPartNo"].Value=((TextBox)DataGrid1.FindControl("Edit_PartNo")).Text;<---error
Very curious, 1st question: Is your computer on? 2nd question: Have you rebooted? just kidding. This looks to me like the control does not exist in the datagrid. The find control method would return a null and then the text property would throw the exception.
If it's the first control in your datagrid row, you can try using DataGrid1.Controls[0].Text instead.
I tried it but still fail. Here is a part Of my code. In fact. the DataGrid only have one row after passing the primary key "ReportId" from another page. But i still don't know how to get the ID parameter inside the DataGrid for updating the data. <%@ Import Namespace="System.Data.OleDb" %> <%@ Import Namespace="System.Data" %> <%@ Page Language="C#" Debug="true" %> <html> <head> <title>----Detail information----</title> <script language="C#" runat="server"> DataSet ds2 = new DataSet(); private void Page_Load(Object Src, EventArgs E) { String ReportID = Request.QueryString["ReportID"]; int len=ReportID.Length-2; ReportID=ReportID.Substring(1,len); String strconn = "Provider=MSDAORA; Data Source=***;User ID=system;Password=***"; String showCmd2 = "select * from SearchingDb where ReportNo='"+ReportID+"'"; OleDbDataAdapter myCommand2 = new OleDbDataAdapter(showCmd2, strconn); myCommand2.Fill(ds2, "SearchingDb"); DataSet2.DataSource = ds2.Tables["SearchingDb"].DefaultView; DataSet2.DataBind(); } private void Save_Data(Object sender, EventArgs E) { String ReportID = Request.QueryString["ReportID"]; OleDbConnection myConnection = new OleDbConnection("Provider=MSDAORA;Data Source=***;User ID=System;Password=***"); String StrSave = "Update SearchingDb set PartNo=? where ReportNo='"+ReportID+"'"; OleDbCommand myCommand = new OleDbCommand(StrSave, myConnection); myCommand.Parameters.Add(new OleDbParameter("@EditPartNo", OleDbType.VarChar, 50)); myCommand.Parameters["@EditPartNo"].Value=((TextBox)DataSet2.FindControl("Edit_PartNo")).Text; error here myConnection.Open(); myCommand.ExecuteNonQuery(); } </script> </head> <body> <form runat="server"> <table> <tr> <td> <asp:Button ID="Save" runat="server" Text="Save" OnClick="Save_Data" /> </td> </tr> <tr><td> <aspataGrid ID="DataSet2" runat="server" DataKeyField="ReportNo" BackColor="#F4FFF4" BorderColor="black" ShowFooter="false" ShowHeader="true" Font-Names="Verdana" CellPadding="3" Cellspacing="0" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" AutoGenerateColumns="false" > <Columns> <asp:TemplateColumn HeaderText="Edit Data" > <ItemTemplate> <table cellpadding="0" cellspacing="0" width="500px" border="1"> <tr> <td> <font color="green" size="2">PartNo:</font> <asp:TextBox ID="Edit_PartNo" Text='<%# DataBinder.Eval(Container.DataItem,"PartNo") %>' Font-Size="X-Small" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <font color="green" size="2">Report-Number:</font><asp:TextBox ID="Edit_ReportNo" Text='<%# DataBinder.Eval(Container.DataItem,"ReportNo") %>' Font-Size="X-Small" ReadOnly="true" runat="server"></asp:TextBox> </td> </tr> </table> </ItemTemplate> </asp:TemplateColumn> </Columns> </aspataGrid> </td></tr> <tr><td><asp:Label ID="Message" runat="server"></asp:Label></td></tr> </table> </form> </body> </html>
You have to iterate through the datagrid's "rows". Your datagrid actually has multiple rows, and each row contains an instance of Edit_PartNo, so you have to do the sql update for each row. See this: http://www.c-sharpcorner.com/Code/2003/Jan/AccessDataGridVal.asp
Thank you for the very useful website: The error was eliminated. Now i can use DataGrid iteration to get each item's value inside DataGrid. However, i found a very strange case. When i try to update the data after changing the TextBox's value. The TextBox only returns the old one. Not updated value. How can i update the data ? foreach(DataGridItem dataGridItem in DataSet2.Items) { String ChangedPartNo = ((TextBox)dataGridItem.FindControl("Edit_PartNo")).Text; String StrSave = "update SearchingDb set PartNo='"+ChangedPartNo+"' where ReportNo='"+ReportID+"'"; OleDbCommand myCommand = new OleDbCommand(StrSave, myConnection); myConnection.Open(); myCommand.ExecuteNonQuery(); } Code (markup):
Is this code called from Page_Load? You should call it from a handler of some sorts (like a button click handler).
yes , i call it from a button click event. But the TextBox "Edit_PartNo" only return the original value. e.g set PartNo='"+ChangedPartNo+"' ---> value of "ChangedPartNo" is not the new value. although i have make a change on it.
in your page_load method, make sure that you check a condition before re-populating the datagrid: if(!IsPostBack){ //code to populate datagrid } If you leave the if-statement out, the datagrid is re-populated on every load of the page, including a postback event such as a button click.