| 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 pointzezo |
|
|
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 pointzezo
See highlighted line.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-16 : 09:16:54
|
| www.sommarskog.se/dynamic_sql.htmlMadhivananFailing to plan is Planning to fail |
 |
|
|
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 |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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. |
 |
|
|
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 |
 |
|
|
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 thisdeclare @maxcount int,@sql varchar(8000)set @sql='select @maxcount=max('+@columen_name+')+1 from counter'exec (@sql)... |
 |
|
|
zez0
Starting Member
24 Posts |
Posted - 2008-06-19 : 03:40:28
|
| sorry but is not work !! this error occureServer 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 |
 |
|
|
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 |
 |
|
|
zez0
Starting Member
24 Posts |
Posted - 2008-06-19 : 04:45:43
|
| any way thank youzezo |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-19 : 05:54:21
|
quote: Originally posted by zez0 any way thank youzezo
declare @sql varchar(8000)set @sql='declare @maxcount int;select @maxcount=max('+@columen_name+')+1 from counter;select @maxcount'exec (@sql)... |
 |
|
|
|