Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys
i have a stored procedure in sql server 2008 which works fine when i execute it in server explorer but when i can't read any data when i call it from my program,here is my code:

DataTable result = null;
            try
            {
                using (con)
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "CheckSite";
                        cmd.Parameters.Add(new SqlParameter("@site", site));
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            result = new DataTable();
                            da.Fill(result);
                            if (result.Rows.Count>0)
                            {
                                
                                return 1;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Pokemon exception handling
            }
            return 0;


what did i do wrong?the database contains 2rows!but my code allways returns 0 (as not finding any)

Here it is my store procedure:

C#
ALTER PROCEDURE dbo.CheckSite
	@site nvarchar(150)
AS
	select sitename from Saved where sitename = @site
	RETURN
Posted
Updated 15-Sep-12 7:29am
v2
Comments
[no name] 15-Sep-12 13:01pm    
Can you improve your question by adding store procedure query?

I think it is a problem in this string:
C#
if (result.Rows.Count>0)

Try to set following code in your procedure:
SQL
SET NOCOUNT OFF;
 
Share this answer
 
try this:
C#
.....
....
...
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
 DataSet result = new DataSet();
 dataset.Tables.Add("aaa");
 da.Fill(dataset, "aaa");
 if (result.Tables[0].Rows.Count>0)
 {

    return 1;
 }
}
........
.......
 
Share this answer
 
v2
Comments
Arash Khangaldi 15-Sep-12 13:37pm    
result.Rows.Count makes error :'System.Data.DataSet' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'System.Data.DataSet' could be found =
[no name] 15-Sep-12 13:42pm    
Because tables have rows, datasets do not.
Kuthuparakkal 15-Sep-12 23:09pm    
updated soln check

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