+ Post New Thread
Results 1 to 4 of 4
Web Development Thread, Must declare the scalar variable "@Name". in Coding and Web Development; Hi I am having an issue with some SQL update code Code: private void ExecuteUpdate(string prjref, string Name, string Description, ...
  1. #1

    Join Date
    Jun 2007
    Location
    Middlesbrough
    Posts
    143
    Thank Post
    17
    Thanked 10 Times in 10 Posts
    Rep Power
    16

    Must declare the scalar variable "@Name".

    Hi

    I am having an issue with some SQL update code

    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("<b>Record Updated.</b><br>");
            
       
        }
    Is resulting in Must declare the scalar variable "@Name".

    Code:
    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("<b>Record Updated.</b><br>");
    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??


  2. #2

    Join Date
    Oct 2005
    Posts
    62
    Thank Post
    7
    Thanked 20 Times in 17 Posts
    Rep Power
    22
    You need to add your parameters collection to your SQLCommand before you do .ExecuteNoneQuery, something like this:

    myCommand.Parameters.Add (sqlparams);



    Peter

  3. #3

    Join Date
    Jun 2007
    Location
    Middlesbrough
    Posts
    143
    Thank Post
    17
    Thanked 10 Times in 10 Posts
    Rep Power
    16
    Cheers PeterH

    Code needed was

    Code:
    myCommand.Parameters.AddRange(sqlparams);

  4. #4

    Join Date
    May 2010
    Posts
    1
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Must declare a scalar variable

    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();

    }


    }

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 2
    Last Post: 11th August 2009, 11:08 AM
  2. "Print Limit Pro" or "Print Managent Plus"
    By burgemaster in forum Windows
    Replies: 24
    Last Post: 10th June 2009, 09:32 AM
  3. "Error 403" & "Moved to here" message
    By tech_guy in forum Windows
    Replies: 4
    Last Post: 24th January 2008, 01:07 PM
  4. Proxy switch "proxy on" & " proxy off" software
    By GavRob in forum Network and Classroom Management
    Replies: 20
    Last Post: 30th July 2007, 10:05 PM
  5. Replies: 6
    Last Post: 28th September 2006, 07:06 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •