5 pts.
Q:
Returning string value from a stored procedure in sql server 2005 to vc# ide code at runtime
Hi,

This is Pushan Chakraborty from Kolkata.I have complied 2 stored procedure to retrieve the names of 2 user defined tables that I have created . The stored procedures are: set



ANSI_NULLS ON set



QUOTED_IDENTIFIER ON

go

  

-- =============================================

-- Author: <Pushan Chakraborty>

-- Create date: <23rd nov>

-- Description: <retrives the name of a particular table>

-- =============================================

ALTER

PROCEDURE [dbo].[select_table1] @tablename char(50) OUTPUT AS

BEGIN



-- SET NOCOUNT ON added to prevent extra result sets from

 

-- interfering with SELECT statements.

 

SET NOCOUNT ON;

 

SELECT @tablename=name FROM sys.tables where object_id ='1275151588'

 

return (@tablename)

 

END





and

set

ANSI_NULLS ON set



QUOTED_IDENTIFIER ON

go

 

 

-- =============================================

-- Author: <Pushan Chakraborty>

-- Create date: <23rd nov 2009>

-- Description: <to retrieve the name of employee table>

-- =============================================

ALTER

PROCEDURE [dbo].[select_table2] @tablename char(50)OUTPUT AS

BEGIN



-- SET NOCOUNT ON added to prevent extra result sets from

 

-- interfering with SELECT statements.

 

SET NOCOUNT ON;

 

SELECT @tablename=name FROM sys.tables where object_id ='1259151531'

 

return(@tablename)

 

END



but the above procedures are not returning any string value but are returning int value by default. the calling code in vc#.net is:

conn.Open();

 

SqlCommand command = new SqlCommand("select_table1", conn);

command.CommandType = CommandType.StoredProcedure;  



string tab1 = command.ExecuteNonQuery(); comboBox3.Items.Add(tab1);

 



SqlCommand command1 = new SqlCommand("select_table2", conn); command1.CommandType =



CommandType.StoredProcedure;  



string tab2 = command1.ExecuteNonQuery(); comboBox3.Items.Add(tab2);

 



MessageBox.Show("comboBox populated"); conn.Close();

 

It is giving me an error message cannot implicitly convert type int to string:--Plz suggest a solution I want to get the table names by calling the stored procedure which is currently not happening

thanks

Pushan







Software/Hardware used:
visual studio 2005 , sql server 2005
ASKED: Nov 26 2009  3:27 PM GMT
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
49385 pts.
A:
 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0
  • Bookmark and Share
By definition, procedures don't return values. If you want to return the result of your query, you would need to use an output parameter.

On the other hand, ExecuteNonQuery returns the number of affected rows (for inserts, updates and deletes) or -1, and that's why you are getting the casting error. In this case, it might be more appropriate to use ExecuteScalar instead.

----------

Stored procedure return values must be numeric. You'll need to pass the object name back as an output parameter or return it as part of a recordset.
Last Answered: Nov 26 2009  11:06 PM GMT by Mrdenny   49385 pts.
Latest Contributors: Carlosdl   32725 pts.
Discuss This Answer:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _