Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Experts,

My stored procedure works fine in SSMS. But, calling from C# it's not working.
my code is as below which returns always -1 : -
C#
.....
.....
ConnectDataBase conDB = new ConnectDataBase();
bool ok = false;
ok = conDB.runQuery("sp_get_unit", txtvalue, "uom");
.....
....

Method in class is as below: -
C#
public bool runQuery( string sp_name, string txtvalue,string paramName)
        {
            int added = 0;
            SqlCommand cmdSeek = new SqlCommand(sp_name, _SqlConnection);
            
            cmdSeek.CommandType = CommandType.StoredProcedure;

            cmdSeek.Parameters.AddWithValue(@paramName, txtvalue);

            try
            {
                _SqlConnection.Open();
                added = cmdSeek.ExecuteNonQuery();
                //rdr = cmdSeek.ExecuteReader();
                
                _SqlConnection.Close();
                if (added > 0)
                    return true;
                else
                    return false;

                //MessageBox.Show(rdr.ToString());
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "ERROR : Invalid/Duplicate/Incomplete data!");
                return false;
            }
            finally
            {
                _SqlConnection.Close();
            }
        }

Thanks,

--- Anil Kumar
Posted
Updated 21-Apr-11 22:05pm
v3

executeNonquery returns an integer specifying the number of rows affected. looks like the SP returns some value.

try using ExecuteScalar
 
Share this answer
 
Try this,

added = cmdSeek.ExecuteScalar();

This will solve your problem and if still you have problem then check your SP whether your Stored Procedure (SP) is returning you right value or query is executing or not.
 
Share this answer
 
Comments
anil_kumar_bhakta 22-Apr-11 4:52am    
thanks. but your expression "added = cmdSeek.ExecuteScalar();" is wrong due to cast type. i already said the SP is working fine in SSMS.
anil_kumar_bhakta 22-Apr-11 6:59am    
By the way, we resolve the problems with slightly changes in this statement.

Thanks a lot.

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