I am trying to get a value of output parameter which I used in my stored procedure, but I am not getting it. I've been trying hard but could not figure it out. The program is not throwing any error, but in the end, the value of _clientCode
is null (Refer screenshot):
I need your support, please help me to find out where I need to change.
public string getData5(string icuser)
{
string _clientCode;
SqlDataReader dataReader;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=xxx-dbprod;DataBase=TCS;Integrated Security=True;";
// conn.ConnectionString = "Data Source = tcr-dbprod; Initial Catalog = TCS; Integrated Security = True;";
//conn.ConnectionString = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "[dbo].[spTCRGetClientCode]";
command.Parameters.AddWithValue("@icUserID", icuser);
command.Parameters.Add(new SqlParameter("@clientCode", SqlDbType.NVarChar, 300));
command.Parameters["@clientCode"].Direction = ParameterDirection.Output;
try
{
conn.Open();
int i = command.ExecuteNonQuery();
dataReader = command.ExecuteReader();
_clientCode = Convert.ToString(command.Parameters["@clientCode"].Value);
conn.Close();
return _clientCode;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
//conn.Close();
//DbConnection.Dispose();
}
}
I explained in my question detail and attached the screenshot for your help.
1条答案
按热度按时间63lcw9qa1#
you can't use ExecuteReader and ExecuteNonQuery for a command,please review your code and use one of ways
you can use two below my code and replace with your code to solve your problem
your code:
this is my code
SqlCommand.ExecuteNonQuery Method
SqlCommand.ExecuteReader Method
Description for
ExecuteNonQuery
andExecuteReader
from ExecuteReader, ExecuteNonQuery and Executescalar in ADO.NET by Mageshwaran RExecuteNonQuery:
ExecuteNonQuery method is used to execute SQL Command or the storeprocedure performs, INSERT, UPDATE or Delete operations. It doesn't return any data from the database. Instead, it returns an integer specifying the number of rows inserted, updated or deleted.
ExecuteReader:
ExecuteReader method is used to execute a SQL Command or storedprocedure returns a set of rows from the database.