Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 unknown error

Author  Topic 

zez0
Starting Member

24 Posts

Posted - 2008-06-16 : 07:01:28
protected void Button4_Click(object sender, EventArgs e)
{
string selectedItem = DropDownList1.SelectedItem.Text;
if (selectedItem == "zezo")
{


string connectionString2 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection connection2 = new SqlConnection(connectionString2);
connection2.Open();
SqlCommand command2 = new SqlCommand("finde", connection2);
command2.CommandType = CommandType.StoredProcedure;
command2.Parameters.Add("@columen_name", SqlDbType.NVarChar).Value = "taher";
command2.ExecuteNonQuery();
}

and this is the stored procedure

ALTER PROCEDURE dbo.finde @columen_name nvarchar(50)
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
declare
@maxcount int,@sql varchar(8000)
set @maxcount=(select max(taher)+1 from counter)

set @sql='insert into counter (' + @columen_name +')
values ( @maxcount )'
exec (@sql)

************************
and this is the error

Server Error in '/NTSOLUTIONS' Application.
--------------------------------------------------------------------------------

Must declare the scalar variable "@maxcount".
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 "@maxcount".

Source Error:


*********************************************
so i can not find where is the exact point

zezo

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-06-16 : 07:11:24
quote:
Originally posted by zez0

protected void Button4_Click(object sender, EventArgs e)
{
string selectedItem = DropDownList1.SelectedItem.Text;
if (selectedItem == "zezo")
{


string connectionString2 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection connection2 = new SqlConnection(connectionString2);
connection2.Open();
SqlCommand command2 = new SqlCommand("finde", connection2);
command2.CommandType = CommandType.StoredProcedure;
command2.Parameters.Add("@columen_name", SqlDbType.NVarChar).Value = "taher";
command2.ExecuteNonQuery();
}

and this is the stored procedure

ALTER PROCEDURE dbo.finde @columen_name nvarchar(50)
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
declare
@maxcount int,@sql varchar(8000)
set @maxcount=(select max(taher)+1 from counter)

set @sql='insert into counter (' + @columen_name +')
values (' + cast(@maxcount as varchar(10)) + ')'
exec (@sql)

************************
and this is the error

Server Error in '/NTSOLUTIONS' Application.
--------------------------------------------------------------------------------

Must declare the scalar variable "@maxcount".
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 "@maxcount".

Source Error:


*********************************************
so i can not find where is the exact point

zezo



See highlighted line.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-06-16 : 09:16:54
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

zez0
Starting Member

24 Posts

Posted - 2008-06-19 : 02:37:14
when i do that an error appear :

Server Error in '/NTSOLUTIONS' Application.
--------------------------------------------------------------------------------

Conversion failed when converting the varchar value 'taher' to data type int.
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: Conversion failed when converting the varchar value 'taher' to data type int.

Source Error:


Line 208: command2.CommandType = CommandType.StoredProcedure;
Line 209: command2.Parameters.Add("@columen_name", SqlDbType.VarChar).Value = "taher";
Line 210: command2.ExecuteNonQuery();
Line 211: }
Line 212:




zezo
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 02:40:04
quote:
Originally posted by zez0

when i do that an error appear :

Server Error in '/NTSOLUTIONS' Application.
--------------------------------------------------------------------------------

Conversion failed when converting the varchar value 'taher' to data type int.
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: Conversion failed when converting the varchar value 'taher' to data type int.

Source Error:


Line 208: command2.CommandType = CommandType.StoredProcedure;
Line 209: command2.Parameters.Add("@columen_name", SqlDbType.VarChar).Value = "taher";
Line 210: command2.ExecuteNonQuery();
Line 211: }
Line 212:




zezo


What's the datatype of @columen_name parameter used in your stored procedure finde? i guess its int and you're trying to pass the string value "taher" into it from application. Hence the error.
Go to Top of Page

zez0
Starting Member

24 Posts

Posted - 2008-06-19 : 02:52:18
no it is no int this is my stired procedure

ALTER PROCEDURE dbo.finde @columen_name varchar(50)
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
declare
@maxcount int,@sql varchar(8000)
set @maxcount=(select max(@columen_name)+1 from counter)

set @sql='insert into counter (' + @columen_name +')
values (' + cast(@maxcount as varchar(10)) + ')'


exec (@sql)

zezo
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 02:56:38
quote:
Originally posted by zez0

no it is no int this is my stired procedure

ALTER PROCEDURE dbo.finde @columen_name varchar(50)
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
declare
@maxcount int,@sql varchar(8000)
set @maxcount=(select max(@columen_name)+1 from counter)
set @sql='insert into counter (' + @columen_name +')
values (' + cast(@maxcount as varchar(10)) + ')'


exec (@sql)

zezo


You're declaring maxcount as int and assigning max(@columen_name) +1 as its value but @columen_name is of type varchar which causes the error.
Go to Top of Page

zez0
Starting Member

24 Posts

Posted - 2008-06-19 : 03:01:58
@columen_name is the name of colum that i will pass it by parameter(like "taher") so it shoul be varcahr

so there is any way to pass this columen name without error ?

zezo
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 03:11:02
quote:
Originally posted by zez0

@columen_name is the name of colum that i will pass it by parameter(like "taher") so it shoul be varcahr

so there is any way to pass this columen name without error ?

zezo


ok in that case you need it like this

declare @maxcount int,@sql varchar(8000)
set @sql='select @maxcount=max('+@columen_name+')+1 from counter'
exec (@sql)
...

Go to Top of Page

zez0
Starting Member

24 Posts

Posted - 2008-06-19 : 03:40:28
sorry but is not work !! this error occure

Server Error in '/NTSOLUTIONS' Application.
--------------------------------------------------------------------------------

Must declare the scalar variable "@maxcount".
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 "@maxcount".

Source Error:


Line 208: command2.CommandType = CommandType.StoredProcedure;
Line 209: command2.Parameters.Add("@columen_name", SqlDbType.VarChar).Value = "taher";
Line 210: command2.ExecuteNonQuery();
Line 211: }
Line 212:



zezo
Go to Top of Page

zez0
Starting Member

24 Posts

Posted - 2008-06-19 : 04:09:30
i tray many time but what is mean Must declare the scalar variable "@maxcount"

i already declear it ???

zezo
Go to Top of Page

zez0
Starting Member

24 Posts

Posted - 2008-06-19 : 04:45:43
any way thank you

zezo
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 05:54:21
quote:
Originally posted by zez0

any way thank you

zezo


declare @sql varchar(8000)
set @sql='declare @maxcount int;select @maxcount=max('+@columen_name+')+1 from counter;select @maxcount'
exec (@sql)...
Go to Top of Page
   

- Advertisement -