【问题标题】:I can't seem to get my Gridview to update to my SQL Server database我似乎无法让我的 Gridview 更新到我的 SQL Server 数据库
【发布时间】:2013-12-04 03:18:32
【问题描述】:

这是我当前为 OnRowUpdating 事件和 SQL 语句更新数据库的代码。它正在引发异常:

System.NullReferenceException:对象引用未设置为 object.error 的实例

代码:

protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    TextBox txtSu = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox1"); // each textbox refers to the Am then Pm day
    TextBox txtSu1 = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox2");// sun pm
    TextBox txtMo = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox3");// mon am
    TextBox txtMo1 = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox4");//mon pm
    TextBox txtTu = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox5");
    TextBox txtTu1 = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox6");
    TextBox txtWe = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox7");
    TextBox txtWe1 = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox8");
    TextBox txtTh = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox9");
    TextBox txtTh1 = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox10");
    TextBox txtFr = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox11");
    TextBox txtFr1 = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox12");
    TextBox txtSa = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox13");
    TextBox txtSa1 = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox14");

    string sql = "UPDATE tblEMPLOYEE SET EmployeeID=@ID, " +
                                     "AvailSun=@AvSu, " +
                                     "AvailSun1=@AvSu1, " +
                                     "AvailMon=@AvMo, " +
                                     "AvailMon1=@AvMo1, " +
                                     "AvailTues=@AvTu, " +
                                     "AvailTues1=@AvTu1, " +
                                     "AvailWedn=@AvWe, " +
                                     "AvailWedn1=@AvWe1, " +
                                     "AvailThurs=@AvTh, " +
                                     "AvailThurs1=@AvTh1, " +
                                     "AvailFri=@AvFr, " +
                                     "AvailFri1=@AvFr1, " +
                                     "AvailSat=@AvSa, " +
                                     "AvailSat1=@AvSa1 " +
                                     "WHERE EmployeeID=@ID";
    CMethods.executeNonQuery(sql, "@ID", txtID.Text, "@AvSu", txtSu.Text, "@AvSu1", txtSu1.Text, "@AvMo", txtMo.Text, "@AvMo1", txtMo1.Text, "@AvTu", txtTu.Text, "@AvTu1", txtTu1.Text, "@AvWe", txtWe.Text, "@AvWe1", txtWe1.Text, "@AvTh", txtTh.Text, "@AvTh1", txtTh1.Text, "@AvFr", txtFr.Text, "@AvFr1", txtFr1.Text, "@AvSa", txtSa.Text, "@AvSa1", txtSa1.Text, "@ID", ID);
    GV.EditIndex = -1;
    fillUsers();
}
private void fillUsers()
{
    GV.DataSource = CMethods.returnTable("SELECT * FROM tblEMPLOYEE WHERE EmployeeID=" + lblEmployee.Text);
    GV.DataBind();
}

在 C 方法中

public static class CMethods
{
    public static DataTable returnTable(String CommandText, params Object[] values)
    {
        SqlConnection con =
        new SqlConnection(ConfigurationManager.ConnectionStrings["Provider"].ConnectionString);
        SqlCommand cmd = new SqlCommand(CommandText, con);
        for (int i = 0; i < values.Length; i += 2)
        {
            cmd.Parameters.AddWithValue((String)values[i], values[i + 1]);
        }
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds, "tbl");
        return ds.Tables["tbl"];
    }

public static bool executeNonQuery(String CommandText, params Object[] values)
{
    bool bln = true;
    SqlConnection con =
    new SqlConnection(ConfigurationManager.ConnectionStrings["Provider"].ConnectionString);
    SqlCommand cmd = new SqlCommand(CommandText, con);
    for (int i = 0; i < values.Length; i += 2)
    {
        cmd.Parameters.AddWithValue((String)values[i], values[i + 1]);
    }
    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        bln = false;

    }
    finally
    {
        con.Close();
    }
    return bln;
}
 public static double returnValue(string str)
{
    double dblTemp = 0.0D;
    string strTemp = String.Empty;
    bool blnFirstDec = false;
    bool blnFirstNeg = false;

    for (int i = 0; i < str.Length; i++)
    {
        if (str.Substring(i, 1) == "-")
        {
            blnFirstNeg = true;
        }
        if (IsNumeric(str.Substring(i, 1)) || str.Substring(i, 1) == ".")
        {
            if (str.Substring(i, 1) == ".")
            {
                if (!blnFirstDec)
                {
                    blnFirstDec = true;
                    strTemp += ".";
                }
            }
            else 
            {
                strTemp += str.Substring(i, 1);
            }
        }
    }

【问题讨论】:

  • 您没有显示代码的所有部分。例如,我们不知道 CMethods 是什么或 fillUsers() 做什么?此外,您使用 executeNonQuery 的方式并不常见。从您所展示的内容中很难猜出哪里出了问题。
  • 重要的部分是你的CMethods.executeNonQuery 方法是如何运作的。请将其包含在您的问题中。
  • 并且请告诉 WHERE EXACTLY(哪一行代码)这个异常发生.....
  • 异常已经修复,现在我的gridview不会更新,但不会抛出错误我不确定是我的SQL语句还是什么。

标签: c# asp.net sql sql-server gridview


【解决方案1】:

如果您不提供正确的控件 ID,FindControl 控件可能不会返回控件

例如你给了txtBo13(缺少x) 但实际上你可能有txtBox13

所以下面的代码为txtSa返回null

TextBox txtSa = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBo13");

当你调用txtSaText 属性时,你会得到NullReferenceException

如果您将 id 指定为txtBox13,请相应更改代码

TextBox txtSa = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox13");

而且你可能忘记设置@student参数值

【讨论】:

  • 谢谢你让我摆脱了 NullReferenceException 错误,我的代码现在运行没有错误但是现在当我点击更新我在我的 gridview 中编辑的文本框的值时不要更新我会发回更多代码
  • @user3064123 您忘记将@student 参数发送到executeNonQuery 方法
  • 我刚刚将其更改为 EmployeeID,因为它的名称不正确,我也会在此处更新代码,但没有必要发送 ID,因为我没有使该项目可编辑我是仅使用它来告诉数据库在我的 sql 语句中更新的位置是否需要在命令字符串中无限制?
  • where条件应该返回true更新记录,所以你需要发送ID参数如"@ID", lblEmployee.Text
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 2016-02-10
  • 2017-12-04
  • 1970-01-01
相关资源
最近更新 更多