Click here to Skip to main content
15,885,720 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

I think the cause is this line
C#
da.UpdateCommand.Parameters.Add("@SerilNo", SqlDbType.NVarChar).Value = txtSerial.Text;

This should be @SerialNo instead of @SerilNo
 
Share this answer
 
Comments
Pete O'Hanlon 7-Mar-12 4:03am    
Exactly right. My 5.
walterhevedeich 7-Mar-12 4:05am    
Thank you Pete.
M.H. Shojaei 7-Mar-12 4:14am    
Ok, thanks :)
 
Share this answer
 
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

C#
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
 
Share this answer
 
v3
Comments
walterhevedeich 7-Mar-12 5:03am    
Care to explain why creating a stored procedure can avoid that type of error?
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
 
Share this answer
 
you make a mistake when adding parameter in update command. Its nothing but a typing mistake.Just change "@SerilNo" to "@SerialNo"
C#
da.UpdateCommand.Parameters.Add("@SerilNo", SqlDbType.NVarChar).Value = txtSerial.Text;

as
C#
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.
 
Share this answer
 
v2
Comments
M.H. Shojaei 8-Mar-12 7:10am    
Thanks all
SQL
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".
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900