I am taking value from textbox1 and trying to pass that value to stored procedure. but while creating stored procedure I am getting below error
Msg 102, Level 15, State 1, Procedure proc_1, Line 8
Incorrect syntax near '@name'.
in c#
string nam = " and city='" + textbox1.Text + "'";
cmd.Parameters.AddWithValue("@name", nam );
create procedure proc_1
(
@name varchar (max)
)
as
begin
select [country] from table_name where city='delhi' @name
end
please let me know, how can I insert the textbox1 value into @name variable
Clive ScottPosted Feb 10, 2013, 4:26 PM
select [country] from table_name where city='delhi' @name
DeepeshPosted Jan 28, 2013, 7:06 PM
When you run the create script you will get the incorrect syntax error because it expects some kind of condition to be applied to it.
try this instead
create procedure proc_1
sabdhi naikPosted Jan 21, 2013, 7:17 AM
This is the procedure for inserting-----
Dorababu MekaPosted Jan 17, 2013, 3:26 AM
Sudhakar ChaudharyPosted Jan 17, 2013, 2:55 AM
Gurjeet SinghPosted Jan 17, 2013, 2:52 AM
But i want to pass and city=' toronto'
select Country from tblCity where CityName='delhi' and city=' toronto'
is it possible.
Or let me know any alternative in stored procedure to validate data...
Sudhakar ChaudharyPosted Jan 17, 2013, 2:47 AM
(
@name varchar (max)
)
as
begin
if(@name is null || '')
select [country] from table_name where city='delhi'
else
select [country] from table_name where city='delhi' OR city=@name
end
Santhosh Kumar JayaramanPosted Jan 17, 2013, 2:40 AM
If @name is not null and @name>''
else
select [country] from table_name where city='delhi'
When passing from UI, make sure if user didnt enter then @name should be passed blank
Dorababu MekaPosted Jan 17, 2013, 2:38 AM
Gurjeet SinghPosted Jan 17, 2013, 2:33 AM
create procedure [dbo].[proc_1](@name varchar(max))
as begin
select Country from tblCity where CityName='delhi''+@name+'
end
suppose
if(textbox1.text="")
{
string nam =" ";
}
else
{
string name= " and city='" + textbox1.Text + "'";
}
cmd.Parameters.AddWithValue("@name", nam );
if TextBox1.text is empty then store procedure will work like this
select Country from tblCity where CityName='delhi''
or if TextBox1.Text is having value then store procedure will work like this
select Country from tblCity where CityName='delhi' and city=' toronto'
Dorababu MekaPosted Jan 17, 2013, 2:32 AM
Try this
create procedure [dbo].[proc_1](@name varchar(max))
as begin
select Country from tblCity where CityName='delhi''+@name+'
end
Gurjeet SinghPosted Jan 17, 2013, 2:26 AM
Gurjeet SinghPosted Jan 17, 2013, 2:25 AM
Dorababu MekaPosted Jan 16, 2013, 8:38 AM
create procedure [dbo].[proc_1](@name varchar(max))
as begin
select Country from tblCity where CityName='delhi''+@name+'
end
but i didn't find any use of this so let us know what you are trying to do
Sudhakar ChaudharyPosted Jan 16, 2013, 8:33 AM
Dorababu MekaPosted Jan 16, 2013, 8:32 AM
Dorababu MekaPosted Jan 16, 2013, 8:23 AM
chellappan pandiarajanPosted Jan 16, 2013, 8:14 AM
change you SQL Proc like below
create procedure proc_1
(
@name varchar (max)
)
as
begin
select [country] from table_name where city in ('delhi', @name)
end
and Change you C# coding like below
if (textbox1.text = "")
{
string name = " ";
}
else
{
string name = "'" + textbox1.Text + "'";
}
it will work.
-Pandian
Dorababu MekaPosted Jan 16, 2013, 8:13 AM
Dorababu MekaPosted Jan 16, 2013, 8:08 AM
Gurjeet SinghPosted Jan 16, 2013, 8:02 AM
if(textbox1.text="")
{
string nam =" ";
}
else
{
string name= " and city='" + textbox1.Text + "'";
}
I am using stored procedure for above purpose
Dorababu MekaPosted Jan 16, 2013, 7:43 AM
Santhosh Kumar JayaramanPosted Jan 16, 2013, 7:43 AM
what your really want to query?
you want to query something like
where city ='Delhi address1' like this??
where address1 is one of the value for @name.
If that is the case.,then try
Gurjeet SinghPosted Jan 16, 2013, 7:41 AM
i want to use only like this :
where city='delhi' @name
Gurjeet SinghPosted Jan 16, 2013, 7:41 AM
i want to use only like this :
where city='delhi' @name
Dorababu MekaPosted Jan 16, 2013, 7:41 AM
SELECT country FROM table_name WHERE
city='Delhi' AND city=@name
Santhosh Kumar JayaramanPosted Jan 16, 2013, 7:33 AM
Gurjeet SinghPosted Jan 16, 2013, 7:32 AM
I want to use only AND condition
Santhosh Kumar JayaramanPosted Jan 16, 2013, 7:31 AM
WHERE city='delhi' OR city=@name
Gurjeet SinghPosted Jan 16, 2013, 7:29 AM
AND city='" + textbox1.Text + " into @name variable.
i know that WHERE condition must be like:
WHERE city='delhi' AND city=@name
Santhosh Kumar JayaramanPosted Jan 16, 2013, 7:22 AM
where city='delhi' @name
it is wrong. it should be
where city=@name