Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL WPF
Hi everyone, my project has this error: Must declare the scalar variable "@SerialNo". What's cause?
thanks
 
private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Tickets.mdf;Integrated Security=True;User Instance=True";
 
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
 
            SqlDataAdapter da = new SqlDataAdapter();
            da.UpdateCommand = new SqlCommand(@"Update [Ticket] Set No = @No, Date = @Date, [From] = @From, [To] = @To, IssueDate = @IssueDate, FlightNo = @FlightNo, ClassBasis = @ClassBasis, Issuer = @Issuer, Code = @Code, Form = @Form, SerialNo = @SerialNo Where No = @No", con);
 

            da.UpdateCommand.Parameters.Add("@No", SqlDbType.Int).Value = (txtNo.Text).ToString();
            da.UpdateCommand.Parameters.Add("@Date", SqlDbType.NVarChar).Value = txtDate.Text;
            da.UpdateCommand.Parameters.Add("@From", SqlDbType.NVarChar).Value = txtFrom.Text;
            da.UpdateCommand.Parameters.Add("@To", SqlDbType.NVarChar).Value = txtTo.Text;
            da.UpdateCommand.Parameters.Add("@IssueDate", SqlDbType.NVarChar).Value = txtIssueDate.Text;
            da.UpdateCommand.Parameters.Add("@FlightNo", SqlDbType.NVarChar).Value = txtFlightNo.Text;
            da.UpdateCommand.Parameters.Add("@ClassBasis", SqlDbType.NVarChar).Value = txtClassBasis.Text;
            da.UpdateCommand.Parameters.Add("@Issuer", SqlDbType.NVarChar).Value = txtIssuer.Text;
            da.UpdateCommand.Parameters.Add("@Code", SqlDbType.NVarChar).Value = txtCode.Text;
            da.UpdateCommand.Parameters.Add("@Form", SqlDbType.NVarChar).Value = txtForm.Text;
            da.UpdateCommand.Parameters.Add("@SerilNo", SqlDbType.NVarChar).Value = txtSerial.Text;
 
            con.Open();
            da.UpdateCommand.ExecuteNonQuery();
            con.Close();
 
            this.ShowData();
Posted 6-Mar-12 21:58pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think the cause is this line
da.UpdateCommand.Parameters.Add("@SerilNo", SqlDbType.NVarChar).Value = txtSerial.Text;
This should be @SerialNo instead of @SerilNo
  Permalink  
Comments
Pete O'Hanlon at 7-Mar-12 4:03am
   
Exactly right. My 5.
walterhevedeich at 7-Mar-12 4:05am
   
Thank you Pete.
Mohammad Hossein Shojaei at 7-Mar-12 4:14am
   
Ok, thanks :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

mistake is @SerialNo instead of @SerilNo
 
hiii guys we can write stored procedure for this we can avoid this type of error...............
 

we can use stored procedures also
 
Create procedure updateproc(@No int,@Date varchar(50)  ,@From varchar(50)  ,@IssueDate varchar(50)  ,@FlightNo varchar(50)   ,@ClassBasis  varchar(50) ,@Issuer varchar(50) ,@SerialNo  varchar(50))
As 
Begin
Update [Ticket] Set No = @No, Date = @Date, [From] = @From, [To] = @To, IssueDate = @IssueDate, FlightNo = @FlightNo, ClassBasis = @ClassBasis, Issuer = @Issuer, Code = @Code, Form = @Form, SerialNo = @SerialNo Where No = @No"
end
  Permalink  
v3
Comments
walterhevedeich at 7-Mar-12 5:03am
   
Care to explain why creating a stored procedure can avoid that type of error?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

when we complile stored procedure....the errors comes like this we can easily find it............Procedure or function 'updateproc' expects parameter '@SerilNo', which was not supplied.
 

other wise we get like this......declare the scalar variable "@SerialNo" this is confusing error shows
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

you make a mistake when adding parameter in update command. Its nothing but a typing mistake.Just change "@SerilNo" to "@SerialNo"
da.UpdateCommand.Parameters.Add("@SerilNo", SqlDbType.NVarChar).Value = txtSerial.Text;

as
da.UpdateCommand.Parameters.Add("@SerialNo", SqlDbType.NVarChar).Value = txtSerial.Text;

And you will find your code will run perfectly.You should check when using string query from back-end.
  Permalink  
v2
Comments
Mohammad Hossein Shojaei at 8-Mar-12 7:10am
   
Thanks all

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 8 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid