public void updateMessage ( object src, EventArgs e )
{
string ServerName = "xxx"; // SQL server name
string Database = "xxx"; // SQL Database Name
string user = "xxx"; // SQL User name
string Pass = "xxx"; // SQL PasswordSqlConnection myConn = new SqlConnection("server=" + ServerName + ";database=" + Database + ";UID=" + user + ";PWD=" + Pass + ";");
SqlCommand myCmd = new SqlCommand ( );SqlDataAdapter myAdapter = new SqlDataAdapter ("select * from cars where ID=" + TheID, myConn );
DataTable msgDetails = new DataTable ( );
myAdapter.Fill ( msgDetails );
DataRowView myRow = msgDetails.DefaultView [ 0 ] ;SqlDataReader rdr;
if ( Page.IsValid )
{
myCmd.CommandText = "UPDATE cars SET Fastnumer=@dotnet.itags.org.Fastnumer, Tegund=@dotnet.itags.org.Tegund where id=" + TheID;msgFastnumer.Value = myRow [ "Fastnumer" ].ToString ( );
msgTegund.Value = myRow [ "Tegund" ].ToString ( );myCmd.Parameters.Add ( "@dotnet.itags.org.Fastnumer", SqlDbType.NVarChar ).Value = msgFastnumer.Value;
myCmd.Parameters.Add ( "@dotnet.itags.org.Tegund", SqlDbType.NVarChar ).Value = msgTegund.Value;myConn.Open ( );
rdr = myCmd.ExecuteReader ( );
myGrid.DataSource = rdr;
myGrid.DataBind ( );
rdr.Close();
myConn.Close ( );}
bindPostPanel ( TheID );
}
ExecuteReader: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: ExecuteReader: Connection property has not been initialized.
Source Error:
Line 140:
Line 141: myConn.Open ( );
Line 142: rdr = myCmd.ExecuteReader ( );
Line 143: myGrid.DataSource = rdr;
Line 144: myGrid.DataBind ( );Well, for starters, you need to set the Connection property of your SqlCommand:
myCmd.Connection = myConn;
0 comments:
Post a Comment