无法插入SQL Server 2012-错误:“”;属性CommandText尚未初始化”;
本文关键字:属性 CommandText 初始化 插入 SQL Server 错误 2012- | 更新日期: 2023-09-27 17:59:16
我看不到错误,但我得到了一个异常:
无法插入员工:ExecuteReader:属性CommandText尚未初始化
代码:
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;
}
}
救命!我正在努力解决这个问题。为什么不插入并给我例外?
您没有在aspx页面中的SQL数据源上设置InsertCommand属性。
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insertcommand(v=vs.110).aspx