Problem on DataGrid.FindControl("")--Object reference not set to an instance of an ob

Discussion in 'C#' started by alanX, Jul 20, 2006.

  1. #1
    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
     
    alanX, Jul 20, 2006 IP
  2. tonyrocks

    tonyrocks Active Member

    Messages:
    1,574
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    88
    #2
    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.
     
    tonyrocks, Jul 20, 2006 IP
  3. adbie

    adbie Peon

    Messages:
    149
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    If it's the first control in your datagrid row, you can try using DataGrid1.Controls[0].Text instead.
     
    adbie, Jul 20, 2006 IP
  4. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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>
    <asp:DataGrid 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>
    </asp:DataGrid>
    </td></tr>
    <tr><td><asp:Label ID="Message" runat="server"></asp:Label></td></tr>
    </table>
    </form>
    </body>
    </html>
     
    alanX, Jul 21, 2006 IP
  5. adbie

    adbie Peon

    Messages:
    149
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    adbie, Jul 22, 2006 IP
  6. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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):
     
    alanX, Jul 22, 2006 IP
  7. benjymouse

    benjymouse Peon

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Is this code called from Page_Load? You should call it from a handler of some sorts (like a button click handler).
     
    benjymouse, Jul 23, 2006 IP
  8. alanX

    alanX Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    alanX, Jul 23, 2006 IP
  9. adbie

    adbie Peon

    Messages:
    149
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    adbie, Jul 26, 2006 IP