Reputation: 4199
I cannot see an error, however I am getting an exception:
Unable to Insert Employee: ExecuteReader: the property CommandText has not been initialized
Code:
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;
}
}
Help! I am battling to sort this out. Why is not inserting and is giving me the exception?
Upvotes: 0
Views: 90
Reputation: 7211
You don't have the InsertCommand property set on your SQL data source in the aspx page.
Upvotes: 2