G
Greg
This is my first crack at ASP.NET and ADO.NET. I am making a page
that will add/change/delete to SQL db using a DataGrid. I found a
sample in a book that does the exact same thing I want using the
Northwind db, but it doesn't work. The select, insert, and delete
commands work fine, but errors out if I uncomment the update code
which is the same format as the others. As soon as I call
DataAdapter.Fill it throws exception. I was under the impression that
this only executes the CommandText for the SqlCommand object set to
the SelectCommand property of the DataAdapter. Can anyone tell me why
this code won't work for the update command?
Here is the error text
Prepared statement '(@CustomerID nchar(5),@ContactName
nvarchar(30),@CompanyName nva' expects parameter @CustomerID, which
was not supplied"
Here's the code for Page_Load:
SqlConnection cnn = new SqlConnection(@"Data Source=<server>; Initial
Catalog=Northwind; Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
//Create SELECT SqlCommand
SqlCommand cmdSelect = cnn.CreateCommand();
cmdSelect.CommandType = CommandType.Text;
cmdSelect.CommandText = "SELECT CustomerID, CompanyName, ContactName
FROM Customers";
//Create UPDATE SqlCommand
SqlCommand cmdUpdate = cnn.CreateCommand();
cmdSelect.CommandType = CommandType.Text;
cmdSelect.CommandText = "UPDATE Customers SET CompanyName =
@CompanyName, ContactName = @ContactName WHERE CustomerID =
@CustomerID";
cmdSelect.Parameters.Add("@CustomerID", SqlDbType.NChar, 5,
"CustomerID");
cmdSelect.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30,
"ContactName");
cmdSelect.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40,
"CompanyName");
cmdSelect.Parameters["@CustomerID"].SourceVersion =
DataRowVersion.Original;
//Create INSERT SqlCommand
SqlCommand cmdInsert = cnn.CreateCommand();
cmdInsert.CommandType = CommandType.Text;
cmdInsert.CommandText = "INSERT INTO Customers (CustomerID,
CompanyName, ContactName) VALUES (@CustomerID, @CompanyName,
@ContactName)";
cmdInsert.Parameters.Add("@CustomerID", SqlDbType.NChar, 5,
"CustomerID");
cmdInsert.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40,
"CompanyName");
cmdInsert.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30,
"ContactName");
cmdInsert.Parameters["@CustomerID"].SourceVersion =
DataRowVersion.Original;
//Create DELETE SqlCommand
SqlCommand cmdDelete = cnn.CreateCommand();
cmdDelete.CommandType = CommandType.Text;
cmdDelete.CommandText = "DELETE FROM Customers WHERE CustomerID =
@CustomerID";
cmdDelete.Parameters.Add("@CustomerID", SqlDbType.NChar, 5,
"CustomerID");
cmdDelete.Parameters["@CustomerID"].SourceVersion =
DataRowVersion.Original;
//Set up SqlDataAdapter
da.SelectCommand = cmdSelect;
da.UpdateCommand = cmdUpdate;
da.InsertCommand = cmdInsert;
da.DeleteCommand = cmdDelete;
da.Fill(ds, "Customers");
//Fill grid on page load
if (!IsPostBack)
{
dgCustomers.DataSource = ds;
dgCustomers.DataMember = "Customers";
dgCustomers.DataBind();
}
}
that will add/change/delete to SQL db using a DataGrid. I found a
sample in a book that does the exact same thing I want using the
Northwind db, but it doesn't work. The select, insert, and delete
commands work fine, but errors out if I uncomment the update code
which is the same format as the others. As soon as I call
DataAdapter.Fill it throws exception. I was under the impression that
this only executes the CommandText for the SqlCommand object set to
the SelectCommand property of the DataAdapter. Can anyone tell me why
this code won't work for the update command?
Here is the error text
Prepared statement '(@CustomerID nchar(5),@ContactName
nvarchar(30),@CompanyName nva' expects parameter @CustomerID, which
was not supplied"
Here's the code for Page_Load:
SqlConnection cnn = new SqlConnection(@"Data Source=<server>; Initial
Catalog=Northwind; Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
//Create SELECT SqlCommand
SqlCommand cmdSelect = cnn.CreateCommand();
cmdSelect.CommandType = CommandType.Text;
cmdSelect.CommandText = "SELECT CustomerID, CompanyName, ContactName
FROM Customers";
//Create UPDATE SqlCommand
SqlCommand cmdUpdate = cnn.CreateCommand();
cmdSelect.CommandType = CommandType.Text;
cmdSelect.CommandText = "UPDATE Customers SET CompanyName =
@CompanyName, ContactName = @ContactName WHERE CustomerID =
@CustomerID";
cmdSelect.Parameters.Add("@CustomerID", SqlDbType.NChar, 5,
"CustomerID");
cmdSelect.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30,
"ContactName");
cmdSelect.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40,
"CompanyName");
cmdSelect.Parameters["@CustomerID"].SourceVersion =
DataRowVersion.Original;
//Create INSERT SqlCommand
SqlCommand cmdInsert = cnn.CreateCommand();
cmdInsert.CommandType = CommandType.Text;
cmdInsert.CommandText = "INSERT INTO Customers (CustomerID,
CompanyName, ContactName) VALUES (@CustomerID, @CompanyName,
@ContactName)";
cmdInsert.Parameters.Add("@CustomerID", SqlDbType.NChar, 5,
"CustomerID");
cmdInsert.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40,
"CompanyName");
cmdInsert.Parameters.Add("@ContactName", SqlDbType.NVarChar, 30,
"ContactName");
cmdInsert.Parameters["@CustomerID"].SourceVersion =
DataRowVersion.Original;
//Create DELETE SqlCommand
SqlCommand cmdDelete = cnn.CreateCommand();
cmdDelete.CommandType = CommandType.Text;
cmdDelete.CommandText = "DELETE FROM Customers WHERE CustomerID =
@CustomerID";
cmdDelete.Parameters.Add("@CustomerID", SqlDbType.NChar, 5,
"CustomerID");
cmdDelete.Parameters["@CustomerID"].SourceVersion =
DataRowVersion.Original;
//Set up SqlDataAdapter
da.SelectCommand = cmdSelect;
da.UpdateCommand = cmdUpdate;
da.InsertCommand = cmdInsert;
da.DeleteCommand = cmdDelete;
da.Fill(ds, "Customers");
//Fill grid on page load
if (!IsPostBack)
{
dgCustomers.DataSource = ds;
dgCustomers.DataMember = "Customers";
dgCustomers.DataBind();
}
}