Datagrid problems

Discussion in 'ASP .Net' started by Douglas Gage, Jan 13, 2004.

  1. Douglas Gage

    Douglas Gage Guest

    I have two columns

    Question Answers
    Q1
    Q2
    Q3
    Q4

    Submit

    I have the following code to collect the answers and update them in the
    database but I got the error. I don't know what to do, please tell me if you
    can

    Thanks







    public void updateQuestion(object sender,System.EventArgs e)

    {

    ArrayList myAnsList = new ArrayList();

    foreach(DataGridItem dataGridItem in grid.Items)

    {

    string str = ((TextBox)grid.FindControl("answerbox")).Text; ->error

    myAnsList.Add(str);

    }

    //update

    loadProDataTalbe(myAnsList);

    }

    <asp:datagrid id="grid" runat="server" >
    <SelectedItemStyle Font-Bold="True" ForeColor="White"
    BackColor="#9471DE"></SelectedItemStyle>
    <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
    BackColor="#4A3C8C"></HeaderStyle>
    <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
    <PagerStyle HorizontalAlign="Right" ForeColor="Black"
    BackColor="#C6C3C6"></PagerStyle>
    <Columns>
    <asp:BoundColumn DataField="Q_Question"
    HeaderText="Questions"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="Answer">
    <ItemTemplate>
    <asp:TextBox Runat="server" ID="answerbox"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>




    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set
    to an instance of an object.

    Source Error:

    Line 100: foreach(DataGridItem dataGridItem in grid.Items)
    Line 101: {
    Line 102: string str = ((TextBox)grid.FindControl("answerbox")).Text;
    Line 103: myAnsList.Add(str);
    Line 104: }
     
    Douglas Gage, Jan 13, 2004
    #1
    1. Advertising

  2. Douglas,
    Looks like you need to check the ListItemType before
    your loop. The DateGrid.Items collection holds the Header
    and Footer items as well as the data that you are interest
    in testing. The code is blowing up on the
    ListItemType.Header and will do the same when it gets down
    to the ListItemType.Footer because those datagrid rows
    don't contain the textbox controls.


    Barry.
    >-----Original Message-----
    >I have two columns
    >
    >Question Answers
    >Q1
    >Q2
    >Q3
    >Q4
    >
    >Submit
    >
    >I have the following code to collect the answers and

    update them in the
    >database but I got the error. I don't know what to do,

    please tell me if you
    >can
    >
    >Thanks
    >
    >
    >
    >
    >
    >
    >
    >public void updateQuestion(object sender,System.EventArgs

    e)
    >
    >{
    >
    > ArrayList myAnsList = new ArrayList();
    >
    > foreach(DataGridItem dataGridItem in grid.Items)
    >
    > {
    >
    > string str = ((TextBox)grid.FindControl

    ("answerbox")).Text; ->error
    >
    > myAnsList.Add(str);
    >
    > }
    >
    >//update
    >
    >loadProDataTalbe(myAnsList);
    >
    >}
    >
    > <asp:datagrid id="grid" runat="server" >
    > <SelectedItemStyle Font-Bold="True"

    ForeColor="White"
    >BackColor="#9471DE"></SelectedItemStyle>
    > <ItemStyle ForeColor="Black"

    BackColor="#DEDFDE"></ItemStyle>
    > <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
    >BackColor="#4A3C8C"></HeaderStyle>
    > <FooterStyle ForeColor="Black"

    BackColor="#C6C3C6"></FooterStyle>
    > <PagerStyle HorizontalAlign="Right"

    ForeColor="Black"
    >BackColor="#C6C3C6"></PagerStyle>
    > <Columns>
    > <asp:BoundColumn DataField="Q_Question"
    >HeaderText="Questions"></asp:BoundColumn>
    > <asp:TemplateColumn HeaderText="Answer">
    > <ItemTemplate>
    > <asp:TextBox Runat="server"

    ID="answerbox"></asp:TextBox>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:datagrid>
    >
    >
    >
    >
    >Object reference not set to an instance of an object.
    >Description: An unhandled exception occurred during the

    execution of the
    >current web request. Please review the stack trace for

    more information
    >about the error and where it originated in the code.
    >
    >Exception Details: System.NullReferenceException: Object

    reference not set
    >to an instance of an object.
    >
    >Source Error:
    >
    >Line 100: foreach(DataGridItem

    dataGridItem in grid.Items)
    >Line 101: {
    >Line 102: string str =

    ((TextBox)grid.FindControl("answerbox")).Text;
    >Line 103: myAnsList.Add(str);
    >Line 104: }
    >
    >
    >.
    >
     
    Barry Forrest, Jan 13, 2004
    #2
    1. Advertising

  3. Douglas Gage

    Douglas Gage Guest

    You are right, I have it fixed Now i want to store all the ansers users type
    in those boxes and store it somewhere. I could not get those answers

    Thank for your help

    "Douglas Gage" <> wrote in message
    news:...
    > I have two columns
    >
    > Question Answers
    > Q1
    > Q2
    > Q3
    > Q4
    >
    > Submit
    >
    > I have the following code to collect the answers and update them in the
    > database but I got the error. I don't know what to do, please tell me if

    you
    > can
    >
    > Thanks
    >
    >
    >
    >
    >
    >
    >
    > public void updateQuestion(object sender,System.EventArgs e)
    >
    > {
    >
    > ArrayList myAnsList = new ArrayList();
    >
    > foreach(DataGridItem dataGridItem in grid.Items)
    >
    > {
    >
    > string str =

    ((TextBox)grid.FindControl("answerbox")).Text; ->error
    >
    > myAnsList.Add(str);
    >
    > }
    >
    > //update
    >
    > loadProDataTalbe(myAnsList);
    >
    > }
    >
    > <asp:datagrid id="grid" runat="server" >
    > <SelectedItemStyle Font-Bold="True" ForeColor="White"
    > BackColor="#9471DE"></SelectedItemStyle>
    > <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
    > <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
    > BackColor="#4A3C8C"></HeaderStyle>
    > <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
    > <PagerStyle HorizontalAlign="Right" ForeColor="Black"
    > BackColor="#C6C3C6"></PagerStyle>
    > <Columns>
    > <asp:BoundColumn DataField="Q_Question"
    > HeaderText="Questions"></asp:BoundColumn>
    > <asp:TemplateColumn HeaderText="Answer">
    > <ItemTemplate>
    > <asp:TextBox Runat="server" ID="answerbox"></asp:TextBox>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > </Columns>
    > </asp:datagrid>
    >
    >
    >
    >
    > Object reference not set to an instance of an object.
    > Description: An unhandled exception occurred during the execution of the
    > current web request. Please review the stack trace for more information
    > about the error and where it originated in the code.
    >
    > Exception Details: System.NullReferenceException: Object reference not set
    > to an instance of an object.
    >
    > Source Error:
    >
    > Line 100: foreach(DataGridItem dataGridItem in grid.Items)
    > Line 101: {
    > Line 102: string str = ((TextBox)grid.FindControl("answerbox")).Text;
    > Line 103: myAnsList.Add(str);
    > Line 104: }
    >
    >
     
    Douglas Gage, Jan 13, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ingeborg

    DataGrid problems

    Ingeborg, Jun 28, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    448
    Luke Zhang [MSFT]
    Jul 1, 2003
  2. Susan Baker
    Replies:
    2
    Views:
    846
    kelvSYC
    Jun 26, 2005
  3. Shelly

    Problems, problems for newbie

    Shelly, Sep 2, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    2,239
    Shelly
    Sep 3, 2007
  4. Andreas Klemt
    Replies:
    0
    Views:
    329
    Andreas Klemt
    Oct 8, 2003
  5. Sak Na rede
    Replies:
    0
    Views:
    437
    Sak Na rede
    Jan 30, 2009
Loading...

Share This Page