Click here to Skip to main content
12,501,105 members (40,443 online)
Rate this:
 
Please Sign up or sign in to vote.
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:

ALTER PROCEDURE dbo.CheckSite
	@site nvarchar(150)
AS
	select sitename from Saved where sitename = @site
	RETURN
Posted 15-Sep-12 6:47am
Updated 15-Sep-12 7:29am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

I think it is a problem in this string:
if (result.Rows.Count>0)
Try to set following code in your procedure:
SET NOCOUNT OFF;
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try this:
.....
....
...
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;
 }
}
........
.......
  Permalink  
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 =
Wes Aday 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 15 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100