Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All,

I want to create one stored procedure in SQL SERVER which will return me string value and also want to create method in C# which will execute this stored procedure and read the string value and stored it in variable.



Please help me with the best approach to do this.
Posted
Updated 7-Jun-12 2:53am
v2
Comments
Maciej Los 7-Jun-12 6:46am    
Not clear! Plese be more specific and provide more details.

The person asking this question has specified the code you need:

http://stackoverflow.com/questions/706361/getting-return-value-from-stored-procedure-in-c-sharp[^]

Just make sure to apply the fix specified in the answers. That will allow you to execute a stored procedure in C#, get the return value, and store it in a variable.
 
Share this answer
 
Comments
Maciej Los 7-Jun-12 11:39am    
It might be it ;)
hi,
Please try this it help you....


public string ExecuteDataSetSP(string spName, Boolean withOutPara)
  {

      SqlDataAdapter daDataAdapter = new SqlDataAdapter();
      DataSet dsDataSet = new DataSet();

      // Initialize the Exception ...

          // Prepare DataAdapter ...
          daDataAdapter.SelectCommand = new SqlCommand();
          daDataAdapter.SelectCommand.Connection = cnnConnection;
          daDataAdapter.SelectCommand.CommandText = spName;
          daDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
          // Pass Parameters ...


              // Add output parameters ...
              daDataAdapter.SelectCommand.Parameters.Add("@o_ErrorMesg", SqlDbType.VarChar, 250).Direction = ParameterDirection.Output;

          // Open Database Connection ...
          cnnConnection.Open();
          // Fill the DataSet ...
          daDataAdapter.Fill(dsDataSet);
          // Read values of Output Parameters

              // Read Values of Output Paramters and Stored into Property
              mErrorMsg = (string)(daDataAdapter.SelectCommand.Parameters["@o_ErrorMesg"].Value);


      return mErrorMsg ;
  }


Your stored procedure contain Output parameter which return string.
 
Share this answer
 
v2
Hi,

There is two ways to solve this problem.

1. Return Output Parameter from Stored Procedure.
http://www.c-sharpcorner.com/UploadFile/rohatash/get-out-parameter-from-a-stored-procedure-in-Asp-Net/[^]

2. Return table from Stored Procedure which will contain only one row and column
as your string data.
C#
//Suppose your command object is cmd and dataset object is dt
SqlDataAdapter sda=new SqlDataAdapter(cmd);
sda.Fill(ds);
string strMyVal=ds.Tables[0].Rows[0][0].toString();
//now strMyVal is ready with your string value returned from database.



All the best..
--AK
 
Share this answer
 

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