Click here to Skip to main content
15,919,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a gridview with 6 columns..while inserting data into gridview i fill first 4 columns means last 2 columns Name and Size allow NULL values. Later while updating the gridview i want to add data to last 2 columns. since they are null im getting error.i tried to solve this but getting Object reference not set to an instance of an object
below is the code..thanks in advc!!
C#

protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
   {

       SqlConnection con = new SqlConnection("Data Source=VAIO-VAIO;Initial Catalog=CMS;Integrated Security=True");

       string name = GridView2.DataKeys[e.RowIndex].Values["Name"].ToString();
       TextBox txtName = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtName");
       string size = GridView2.DataKeys[e.RowIndex].Values["Size"].ToString();
       TextBox txtSize = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtSize");


           con.Open();
           SqlCommand cmd = new SqlCommand("update Tab2 set Name='" + txtName + "' and  Size= '" + txtSize + "' ", con);
           cmd.ExecuteNonQuery();
           con.Close();
           //lblresult.ForeColor = Color.Green;
           //lblresult.Text = sitename + " Details Updated successfully";
           GridView2.EditIndex = -1;
           GridView2.DataBind();
   }


HTML

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
     Width="1132px" AllowPaging="True" 
     onrowediting="EditCustomer"
onrowupdating="UpdateCustomer"  onrowcancelingedit="CancelEdit"
      
      DataKeyNames="name, size"  >
     <PagerStyle ForeColor="Black" HorizontalAlign="Center" 
               BackColor="#C6C3C6"></PagerStyle>




        <Columns>
         
          <%-- <asp:TemplateField HeaderText="Stockcat">
                                <ItemTemplate>
                                    <asp:DropDownList ID="DropDownList1" runat="server" 
                                     AutoPostBack="True">
                                   
                                    <asp:ListItem Value="des" Text="des" />
                                    <asp:ListItem Value="bms" Text="bms" />
                                   
                                    </asp:DropDownList>

</ItemTemplate>  

</asp:TemplateField> --%> 
                   <asp:BoundField DataField="Stockcat" HeaderText="Stockcat" 
                SortExpression="Stockcat" />
           

            <asp:BoundField DataField="Stockid" HeaderText="Stockid" 
                SortExpression="Stockid" />
           
           
            <asp:BoundField DataField="Company" HeaderText="Company" 
                SortExpression="Company" />
                 <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />

           <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Name">
    <ItemTemplate>
        <asp:Label ID="lblName" runat="server"
            Text='<%# Eval("Name")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtName" runat="server"
            Text='<%# Eval("Name")%>'></asp:TextBox>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>

 <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Size">
    <ItemTemplate>
        <asp:Label ID="lblSize" runat="server"
            Text='<%# Eval("Size")%>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtSize" runat="server"
            Text='<%# Eval("Size")%>'></asp:TextBox>
    </EditItemTemplate> 
    <FooterTemplate>
        <asp:TextBox ID="txtSize" runat="server"></asp:TextBox>
    </FooterTemplate>
</asp:TemplateField>
           
           
           
        <%--     <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Size" HeaderText="Size" SortExpression="Size" />   --%>
           
           
                
        
            <asp:CommandField HeaderText="Update" ShowEditButton="True" />
                 
                 </Columns>
    </asp:GridView>
Posted
Comments
Richard Deeming 29-Jan-15 14:37pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
ZurdoDev 29-Jan-15 16:02pm    
Which line of code is the problem? Your description doesn't really make sense.
Member 11377180 30-Jan-15 1:39am    
string name = GridView2.DataKeys[e.RowIndex].Values["Name"].ToString();
this line gives error object reference not set to an instance of an object
ZurdoDev 30-Jan-15 7:23am    
Just check for null first.

1 solution

before creating the Update query, check whether gridview's column is not null or empty then only create update query for these two last columns.
 
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