我在此代码中遇到gridview更新问题。更新命令不起作用。 [英] i am getting problem in this code for gridview update. The update command is not working.

查看:50
本文介绍了我在此代码中遇到gridview更新问题。更新命令不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Grdviewcategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int userid = Convert.ToInt32(Grdviewcategory.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)Grdviewcategory.Rows[e.RowIndex];
        Label lblID = (Label)row.FindControl("lblID");
        TextBox TextBox1 = (TextBox)row.FindControl("TextBox1");
        FileUpload FileUploadcontrol = (FileUpload)row.FindControl("FileUploadcontrol");
        TextBox TextBox3 = (TextBox)row.FindControl("TextBox3");
         FileUploadcontrol.SaveAs(Server.MapPath("~/uploads/" + FileUploadcontrol.FileName));
        string  path = "//uploads//" + FileUploadcontrol.FileName.ToString();

        Grdviewcategory.EditIndex = -1;

        string conn = System.Configuration.ConfigurationManager.ConnectionStrings["local"].ConnectionString;
        SqlConnection objsqlconn = new SqlConnection(conn);

        SqlCommand cmd = new SqlCommand("update category1 set name='" + TextBox1.Text + "',picture='" + "path" + "',description='" + TextBox3.Text + "'where id='" + userid + "'", objsqlconn);
        objsqlconn.Open();
        cmd.ExecuteNonQuery();
        objsqlconn.Close();
        Grdviewcategory.EditIndex = -1;
       
            gvbind();
        }

推荐答案

标记是......

Markup is...
<asp:GridView ID="Grdviewcategory" runat="server" AutoGenerateColumns="False" DataKeyNames="id" 

          OnPageIndexChanging="Grdviewcategory_PageIndexChanging" OnRowDeleting="Grdviewcategory_RowDeleting" OnRowEditing="Grdviewcategory_RowEditing" OnRowCancelingEdit="Grdviewcategory_RowCancelingEdit" OnRowUpdating ="Grdviewcategory_RowUpdating"  style="margin-top: 0px" Width="591px" >
    <columns>
        <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
        <asp:BoundField DataField="picture" HeaderText="picture" SortExpression="picture" />
        <asp:BoundField DataField="description" HeaderText="description" SortExpression="description" />
        <asp:CommandField ShowEditButton="True" />
        <asp:CommandField ShowDeleteButton="True" />
    </columns>



这里没有这样的 FileUpload 控件存在于Row上。所以,它会抛出 NullReferenceException



更新



在Columns标签内为 FileUpload 添加另一个 TemplateField 列,如...


Here there is no such FileUpload control present on the Row. So, it is throwing you the NullReferenceException.

Update


Add another TemplateField column for FileUpload inside the Columns tag like...

<asp:TemplateField>

    <EditItemTemplate>
    
        <asp:FileUpload id="FileUploadcontrol" runat="server" />
    
    </EditItemTemplate>
</asp:TemplateField


这篇关于我在此代码中遇到gridview更新问题。更新命令不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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