如何在Gridview中更新具有NULL值的列? [英] how to Update columns having NULL values in Gridview?

查看:45
本文介绍了如何在Gridview中更新具有NULL值的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含6列的gridview ...当将数据插入gridview时,我填充前4列意味着最后2列Name和Size允许NULL值。稍后在更新gridview时,我想将数据添加到最后2列。因为他们是null我得到错误。我试图解决这个问题但是将对象引用设置为对象的实例

以下是代码..谢谢advc !!

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!!


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();
   }








<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>

推荐答案

在创建Update查询之前,检查gridview的列是否为空或空然后只为这两个最后一列创建更新查询。
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.


这篇关于如何在Gridview中更新具有NULL值的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆