I have installed the web matrix and worked through the tutorials. I am currently trying to do the tutorials on creating an application.
I am using c# and created the following default.aspx according to the instructions. When I try to run the default page in the webserver, I get a "specified cast is not valid." error. I searched through the archives and found information on changing the webmatrix.exe.config and the webserver.exe.config to att a startup tag to force the matrix to access the proper version of the .net framework.
I made these changes and still receive the error. Can anyone help me? The code is below with the error point marked.
I don't know for sure what's going on, but you should be able to fix it by doing this:
private void BindMasterGrid() {// TODO: Update the ConnectionString and CommandText values for your application
string ConnectionString = "server=(local);database=Orders;Integrated Security=SSPI";
string CommandText = "select OrderID, OrderDate, CustomerName from Orders";SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);DataSet ds = new DataSet();
myCommand.Fill(ds);MasterGrid.DataSource = ds;
MasterGrid.DataBind();
}private void BindDetailGrid() {
// get the filter value from the master Grid's DataKeys collection
if (MasterGrid.SelectedIndex != -1) {
// TODO: update the ConnectionString value for your application
string ConnectionString = "server=(local);database=Orders;Integrated Security=SSPI";// TODO: update the CommandText value for your application
** this is where the error takes place. This line was autogenerated by the matrix and I have changed nothing. The error is: specified cast is not valid.
string filterValue = ((string) MasterGrid.DataKeys[MasterGrid.SelectedIndex]).Replace("'", "''");
string CommandText = "select OrderDetailID, ProductName, Quantity, UnitPrice from OrderDetails where OrderID = '" + filterValue + "'";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlCommand myCommand = new SqlCommand(CommandText, myConnection);myConnection.Open();
DetailsGrid.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}DetailsGrid.DataBind();
}
string filterValue = (MasterGrid.DataKeys[MasterGrid.SelectedIndex].ToString());IOW, use ToString() to perform the casting then break the Replace method off into another line. Works for me, anyway. I'll ask, see if anyone knows why this is.
filterValue = filterValue.Replace("'", "''");
Thank you for your help. My gut reaction was to try .ToString(), but I didn't know if that was the same thing as the cast (string). You're right. It worked fine this way. Thanks so much for answering.
Got the answer, I think. One of my colleagues said this:
"The DataKeys collection doesn't necessarily contain objects of type String. It depends on the data type of the key field specified by the DatatKeyField property. If the key field is an integer value, then the DataKeys collection contains Int32 objects. That's why the ToString() method always works, but casting doesn't. ToString() always returns the string representation of the key value.
Sounds like that's a bug in Web Matrix. It's assuming that the key field is a string, which may not always be the case."
I suppose it is a bug, then; it doesn't get uncovered, however, unless (as here) we change the database that the template points to. Presumably it works with Pubs. (?)
0 comments:
Post a Comment