Nel

Nel

  • NA
  • 716
  • 1.1m

Problem: ExecuteScalar returns nothing in cs file

Apr 17 2011 12:56 PM
Hello,

I call a stored procedure in my cs file, but command.ExecuteScalar() returns nothing, the TextBox which should receive the value remain empty.
If I put certain values for the parameters, eg. "Value1" and "Value2" the procedure performs the calculation, but when I call it in the code with the values I get before calling the procedure, and use them as parameters, I got nothing as a result.

Does anybody has an idea what could be the problem?
Here is the code:
SqlConnection linpostConnection = new SqlConnection();
  DataSet linpostDataSet = new DataSet();
  SqlDataAdapter linpostSqlDataAdapter = new SqlDataAdapter();
  private void CreateDataSet2()
  {
 
  linpostConnection.ConnectionString = linpostSqlDataSource.ConnectionString;
  linpostSqlDataAdapter.SelectCommand = new
  SqlCommand(linpostSqlDataSource.SelectCommand, linpostConnection);
  linpostSqlDataAdapter.Fill(linpostDataSet);
  }
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  CreateDataSet2();
  String textpost = "String1, String2";
  Int32 index=0;
  String pres1="";
  Int32 counter= 2;
  int min = int.MaxValue;;
 
  Object pomalor;
  if (counter >= 1)
  {
  Lab1:
  if (counter > 1)
  {
  index = Convert.ToInt32(textpost.IndexOf(","));
  pres1 = textpost.Substring(0, index - 1);
  textpost = textpost.Substring(index + 1, textpost.Length - (index + 1));
  TextBox3.Text=textpost;
 
  }
  else if (counter == 1)
  {
  pres1 = textpost.Substring(0, textpost.Length);
  }
 
  TextBox1.Text = pres1; //this gets value "Value1", the expected value from above
  TextBox2.Text = "String3";
  TextBox5.Text = counter.ToString();
 
  SqlCommand distpostSqlCommand1 =
  new SqlCommand("Procedure1", linpostConnection);
  distpostSqlCommand1.CommandType = CommandType.StoredProcedure;
  SqlParameter postParameter1 = new SqlParameter
  ("@Ime1", SqlDbType.NVarChar, 60);
  postParameter1.Direction = ParameterDirection.Input;
  postParameter1.Value = TextBox1.Text; // * if I put a certain string value here, for example "Value1" it works fine

  distpostSqlCommand1.Parameters.Add(postParameter1);
  SqlParameter postParameter2 = new SqlParameter
  ("@Ime2", SqlDbType.NVarChar, 60);
  postParameter2.Direction = ParameterDirection.Input;
  postParameter2.Value = TextBox2.Text;
  distpostSqlCommand1.Parameters.Add(postParameter2);
  linpostConnection.Open();
  if (distpostSqlCommand1.ExecuteScalar() != DBNull.Value)
  {
  pomalor = distpostSqlCommand1.ExecuteScalar();
  TextBox4.Text = pomalor.ToString();
  }
 
  linpostConnection.Close();
  if (counter > 1)
  {
 
  counter--;
  goto Lab1;
  }
  }
  }
so, if I put the value that textbox1 gets (the string which can be seen when viewing the site in browser), it works fine. But if I put TextBox1.text or pres1 as a value in the row marked with //*, the stored procedure returns nothing, eventhough I have previously tested it in SQL and it returned a certain value.

Where do I make a mistake, can anybody help me please?

Thank you very much in advance.

Answers (4)