C# 无法插入SQL Server 2012-错误:“0”;属性CommandText尚未初始化";

C# 无法插入SQL Server 2012-错误:“0”;属性CommandText尚未初始化";,c#,asp.net,sql-server,insert,C#,Asp.net,Sql Server,Insert,我看不到错误,但我得到一个异常: 无法插入Employee:ExecuteReader:属性CommandText尚未初始化 代码: 救命啊!我正在努力解决这个问题。为什么不插入并给我异常?您没有在aspx页面的SQL数据源上设置InsertCommand属性 private DataTable _employees; private System.Data.SqlClient.SqlDataAdapter _adapter; private DataSet _ds; private Dat

我看不到错误,但我得到一个异常:

无法插入Employee:ExecuteReader:属性CommandText尚未初始化

代码:


救命啊!我正在努力解决这个问题。为什么不插入并给我异常?

您没有在aspx页面的SQL数据源上设置InsertCommand属性

private DataTable _employees;
private System.Data.SqlClient.SqlDataAdapter _adapter;
private DataSet _ds;

private DataTable Employees
{
    get 
    {
        if (_employees != null)
           return _employees;
        else
        {
            _adapter = new SqlDataAdapter("SELECT * FROM Employees", Convert.ToString(ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"]));
            _ds = new DataSet();
            _adapter.Fill(_ds);
            _ds.Tables[0].TableName = "Employees";
            _employees = _ds.Tables[0];
            return _employees;
        }
    }
}

protected Hashtable collectFormData (Telerik.Web.UI.GridCommandEventArgs e)
{
    UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
    Hashtable newValues = new Hashtable();

    newValues["Country"] = (userControl.FindControl("TextBox7") as TextBox).Text;
    newValues["City"] = (userControl.FindControl("TextBox8") as TextBox).Text;
    newValues["Region"] = (userControl.FindControl("TextBox9") as TextBox).Text;
    newValues["HomePhone"] = (userControl.FindControl("HomePhoneBox") as RadMaskedTextBox).Text;
    newValues["BirthDate"] = (userControl.FindControl("BirthDatePicker") as RadDatePicker).SelectedDate.ToString();
    newValues["TitleOfCourtesy"] = (userControl.FindControl("ddlTOC") as DropDownList).SelectedItem.Value;
    newValues["Notes"] = (userControl.FindControl("TextBox1") as TextBox).Text;
    newValues["Address"] = (userControl.FindControl("TextBox6") as TextBox).Text;
    newValues["FirstName"] = (userControl.FindControl("TextBox2") as TextBox).Text;
    newValues["LastName"] = (userControl.FindControl("TextBox3") as TextBox).Text;
    newValues["HireDate"] = (userControl.FindControl("HireDatePicker") as RadDatePicker).SelectedDate.ToString();
    newValues["Title"] = (userControl.FindControl("TextBox4") as TextBox).Text;

    return newValues;
}

protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
    GridEditableItem editItem = e.Item as GridEditableItem;
    UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
    DataRow newRow = this.Employees.NewRow();
    Hashtable newValues = collectFormData(e);

    newValues["EmployeeID"] = (int)this.Employees.Rows[this.Employees.Rows.Count - 1]["EmployeeID"] + 1;
    _adapter.InsertCommand = new SqlCommand(SqlDataSource1.InsertCommand, new SqlConnection(Convert.ToString(ConfigurationManager.ConnectionStrings["NORTHWNDConnectionString"])));
    try
    {
        foreach (DictionaryEntry entry in newValues)
        {
            newRow[(string)entry.Key] = entry.Value;
            _adapter.InsertCommand.Parameters.Add(new SqlParameter((string)entry.Key, entry.Value));

            _adapter.InsertCommand.Parameters.Add(new SqlParameter("ReportsTo", SqlInt32.Null));
            _adapter.InsertCommand.Parameters.Add(new SqlParameter("PhotoPath", SqlString.Null));
            _adapter.InsertCommand.Parameters.Add(new SqlParameter("Extension", SqlString.Null));
            _adapter.InsertCommand.Parameters.Add(new SqlParameter("PostalCode", SqlString.Null));

        }
        this.Employees.Rows.Add(newRow);
        _adapter.InsertCommand.CommandType = CommandType.Text;
        _adapter.Update(_ds, "Employees");
    }
    catch (Exception ex)
    {
        RadGrid1.Controls.Add(new LiteralControl("Unable to insert Employees. Reason: " + ex.Message));
        e.Canceled = true;
    }
 }