Nel

Nel

  • NA
  • 716
  • 1m

Stored procedure returns empty, which is not true when testing it with the same values in SQL Server Management Studio

Mar 22 2011 4:47 PM
 
Hello,
 
I have a strored procedure which when I test in Sql Server Management Studio returns value for example 1980.19712674283 for the same values I call it in code behind.
But when calling in code behind it returns empty (I got an error DBNull can not be converted...) which is not true. Below I post part of the code and I use the textboxes to be sure that the values are exactly the same I expect to be. But I can not discover why I get empty in the code.
Here is the code:

if (counter > 1)
                            {
                                index = Convert.ToInt32(textpost.IndexOf(","));
                                pres1 = textpost.Substring(0, index-1);
                            }
                            else
                            {
                                pres1 = textpost.Substring(0, textpost.Length);
                            }

                           TextBox7.Text = pres1;
                            TextBox8.Text = pList1.SelectedItem.Value.Trim();

                           SqlCommand distpostSqlCommand1 =
                            new SqlCommand("Procedure1", linpostConnection);
                            distpostSqlCommand1.CommandType = CommandType.StoredProcedure;


                            SqlParameter postParameter1 = new SqlParameter
                                ("@Ime1", SqlDbType.NVarChar, 60);
                            postParameter1.Direction = ParameterDirection.Input;
                            postParameter1.Value = pres1;
                            distpostSqlCommand1.Parameters.Add(postParameter1);

                            SqlParameter postParameter2 = new SqlParameter
                                ("@Ime2", SqlDbType.NVarChar, 60);
                            postParameter2.Direction = ParameterDirection.Input;
                            postParameter2.Value = pList1.SelectedItem.Value.Trim();
                            distpostSqlCommand1.Parameters.Add(postParameter2);

                            linpostConnection.Open();

                            SqlDataReader distSqlDataReader;
                            distSqlDataReader = distpostSqlCommand1.ExecuteReader();

                            while (distSqlDataReader.Read())
                            {
                                //if (distSqlDataReader[1] != DBNull.Value)
                                //{
                                    Int32 pomalor = Convert.ToInt32(distSqlDataReader[1]);

in the last line I got the error "Object can not be cast from DBNull.

Here is what I get as a result from the stored procedure in SQL;

I have two columns:

Ime1               Distance in
Mc Donalds      1980.19712674283

I can not understand from where I get this DBNull when viewing the site in web browser. Can anybody help me please?

Thank you very much in advance.


Answers (2)