Jump to content

Recommended Posts

Posted

Hi

 

I am having an issue with some SQL update code

 

private void ExecuteUpdate(string prjref, string Name, string Description, string SubCat, string StrFileName)
   {
       Response.Write(prjref);
       Response.Write(Name);
       Response.Write(Description);
       Response.Write(SubCat);
       Response.Write(StrFileName);

       SqlParameter[] sqlparams = {
         new SqlParameter("@Name",Name), 
         new SqlParameter("@Description",Description), 
         new SqlParameter("@FileName",StrFileName),
         new SqlParameter("@Sub",SubCat),
         new SqlParameter("@Ref",prjref)
       };

       SqlConnection conn = new SqlConnection(GetConnectionString());
       String updateCmd = "UPDATE ework.Knowledge_Request SET PRJNAME = @Name,  PRJOBJECTIVES = @Description, PRJCLIP1 = @FileName, SubCategory = @Sub  WHERE PRJREF = @Ref";
      

       SqlCommand myCommand = new SqlCommand(updateCmd, conn );
       myCommand.Connection.Open();
       myCommand.ExecuteNonQuery();
           Response.Write("Record Updated.
");
       
  
   }

 

Is resulting in Must declare the scalar variable "@Name".

 

Server Error in '/KnowledgeBase' Application.
--------------------------------------------------------------------------------

Must declare the scalar variable "@Name". 
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.Data.SqlClient.SqlException: Must declare the scalar variable "@Name".

Source Error: 


Line 147:        SqlCommand myCommand = new SqlCommand(updateCmd, conn );
Line 148:        myCommand.Connection.Open();
Line 149:        myCommand.ExecuteNonQuery();
Line 150:            Response.Write("Record Updated.
");
Line 151:        


Source File: h:\KnowledgeBase\Edit.aspx.cs    Line: 149 

Stack Trace: 


[sqlException (0x80131904): Must declare the scalar variable "@Name".]
  System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950890
  System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846875
  System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
  System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
  System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
  System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +317
  System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
  Edit.ExecuteUpdate(String prjref, String Name, String Description, String SubCat, String StrFileName) in h:\KnowledgeBase\Edit.aspx.cs:149
  Edit.UpdateKB(Object sender, EventArgs e) in h:\KnowledgeBase\Edit.aspx.cs:163
  System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
  System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
  System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
  System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082 

 

Any ideas??

 

:D

Posted

You need to add your parameters collection to your SQLCommand before you do .ExecuteNoneQuery, something like this:

 

myCommand.Parameters.Add (sqlparams);

 

 

 

Peter

  • 9 months later...
Posted

Code:

 

public void insertUsercert(byte [] cert,string Name)

{

 

SqlConnection conn = new SqlConnection(cons);

try

{

conn.Open();

}

catch (Exception ex)

{

MessageBox.Show("Exception" + ex.ToString());

 

}

 

 

SqlCommand cmd = new SqlCommand();

SqlParameter cert2 = new SqlParameter("@cert1", System.Data.SqlDbType.VarBinary, cert.Length);

cert2.Value = cert;

cmd.Parameters.Add(cert2);

cmd.CommandText = @"Update [user] Set UserCertificate=@cert1" + "where UserName='" + Name + "'"; //error at the @cert1

 

cmd.Connection = conn;

cmd.ExecuteNonQuery();

 

conn.Close();

 

}

 

 

}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...