我在这里得到空引用异常.SqlCommand cmd = new SqlCommand(" update category1 set name ='" + TextBox1.Text +"',picture ='" +" path" +"',description = '" + TextBox3.Text +"'其中...... [英] I am getting null reference exception here.SqlCommand cmd = new SqlCommand("update category1 set name='" + TextBox1.Text + "',picture='" + "path" + "',description='" + TextBox3.Text + "'where...

查看:98
本文介绍了我在这里得到空引用异常.SqlCommand cmd = new SqlCommand(" update category1 set name ='" + TextBox1.Text +"',picture ='" +" path" +"',description = '" + TextBox3.Text +"'其中......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

所以请使用调试器。



在该行上放置一个断点,然后运行您的应用程序。然后使用鼠标检查teh行的每个部分,以确切地确定哪个部分是 null 。然后你可以回顾一下你的代码并找出 为什么 它是 null



我们不能这样做 - 我们无法访问您的代码,您的数据或您执行该代码的方式,所有这些都可能是相关的。



但是,请不要这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。它赢了;但是你现在的问题很简单,但它可能会稍后保存你的数据库...
So use the debugger.

Put a breakpoint on the line, and run your application. Then use the mouse to examine each part of teh line to identify exactly which part is null. You can then look back though your code and work out why it is null.

We can't do that - we don't have access to you code, your data, or how you got to execute that code, and all of those are probably relevant.

But, please, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. It won;t cuire your current problem, but it might just save your database later...


根据你的上一个问题你的网格在有限的列之下是

as per your last question your grid havin below bounded columns
<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>



没有Label或TextBox控件。所有FindControl方法都将返回空值。



如果你需要获得BoundField的值,请尝试如下


There is no Label or TextBox controls. All your FindControl methods will return null values.

if you need to get the value of BoundField, try like below

string id = row.Cells[0].Text;
string name = row.Cells[1].Text;
string picture = row.Cells[2].Text;
string description = row.Cells[3].Text;


已在我在此代码中遇到gridview更新问题。更新命令不起作用。 [ ^ ]。


这篇关于我在这里得到空引用异常.SqlCommand cmd = new SqlCommand(&quot; update category1 set name ='&quot; + TextBox1.Text +&quot;',picture ='&quot; +&quot; path&quot; +&quot;',description = '&quot; + TextBox3.Text +&quot;'其中......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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