我似乎无法让我的Gridview更新到我的SQL Server数据库

时间:2013-12-04 03:18:33

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

这是我当前用于更新数据库的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();
}

在Cmethods

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

1 个答案:

答案 0 :(得分:0)

如果未提供正确的控件ID,则FindControl控件可能无法返回控件

例如,您提供了txtBo13(缺少x)但实际上您可能有txtBox13

所以下面的代码为txtSa

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

当您致电Text的{​​{1}}财产时,您将获得txtSa

如果您将ID设为NullReferenceException,请相应更改代码

txtBox13

您也可能忘记设置TextBox txtSa = (TextBox)GV.Rows[e.RowIndex].FindControl("txtBox13"); 参数值