Click here to Skip to main content
12,291,271 members (61,942 online)
Rate this:
 
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 7-Mar-12 4:03am
   
Exactly right. My 5.
walterhevedeich 7-Mar-12 4:05am
   
Thank you Pete.
Mohammad Hossein Shojaei 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 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 8-Mar-12 7:10am
   
Thanks all
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

create trigger trig on city1
for insert
as
begin
declare @id int;
declare @name nvarchar(50);
declare @city nvarchar(50);
declare @adres nvarchar(50);
declare @indate datetime;
 
select @id=i.id from inserted i;
select @name =i.name from inserted i;
select @city= i.city from inserted i;
select @adres='after trigger fired';
select @indate='after trigger fired';
 
insert into city11 (id,name,city,adres,indate) values(@id,@name,@city,@adres,getdate());
 
end
 
Error:
 
Must declare the scalar variable "@id".
  Permalink  
Comments
CHill60 16-Jan-15 4:39am
   
What does this have to do with the original post (that is nearly 3 years old and resolved) ???

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160518.1 | Last Updated 16 Jan 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100