views:

129

answers:

2

I have a ASP.NET web page that needs to make a SQL Server call from the client side in a script sectipn of my aspx file. I'm calling a stored proc which takes one parm. This sp works fine in SQL Server Management Studio returning records as expected. When I try to fill a dataset from a call to this sp the ds gets filled with zero records. Is there something about making this call from the client side which I'm missing. Here is my code. I'm hard coding the parm for test purposes. PS - I have this in a try catch and get no errors just an empty dataset.Thnx.

            string strAssignedTo = "Dwight Shoemaker";



            System.Data.DataSet ds = new System.Data.DataSet();
            System.Data.SqlClient.SqlConnection sqlcon = new System.Data.SqlClient.SqlConnection("Data Source=sql394.mysite4now.com;Initial Catalog=ULS_db1;User ID=uls2008;Password=uls2008");
            System.Data.SqlClient.SqlCommand comand = new System.Data.SqlClient.SqlCommand();
            comand.Connection = sqlcon;
            comand.CommandText = "GetAssignedToReport";
            comand.CommandType = System.Data.CommandType.StoredProcedure;

            comand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@assignedTo", System.Data.SqlDbType.VarChar, 50));
            comand.Parameters["@assignedTo"].Value = strAssignedTo;

            System.Data.SqlClient.SqlDataAdapter sqladp = new System.Data.SqlClient.SqlDataAdapter(comand);

            sqlcon.Open();
            sqladp.Fill(ds);
+1  A: 

Hi I've tried your code and it was ok. On thing that might be wrong is this: If you try to bind gridView like: gridView.DataSource= ds; you will see nothing. I've tried your example but instead of previous code, i've bind the gridView like this: gridView.DataSource = ds.Tables[0]; and i see your records. Column name: assigned_dt, with values like: 7/5/2005, 5/2/2005. Is this what you expect. Regards

ZokiManas
Yes - I see the records now. I tried your suggestion. I am setting the datasource for a Crystal Report - but still no records show when displayed. Any thoughts. I created the report template using a dataset (xsd file). Here is my code which does not work:ULS_Site.Reports.EquipAssinedTo oRpt = new ULS_Site.Reports.EquipAssinedTo();oRpt.SetDataSource(ds.Tables[0]);CrystalReportViewer1.ReportSource = oRpt;
MikeD
I just tried a simple test and it works. It must be that my dataset definition is screwed up somehow. I'm joining about 5 tables so I'll take a look at that. Thanks ZM.
MikeD
Just a personal preference that I have when I have to deal with ADO.Net: use DataTable when you know your result uses one select and use dataset when you are using a query with multiple selects.
Min
I redid my dataset template definition to be my stored proc vs. the actual tables and doing the relationships within the report. This seems to work. My question problem now - which I was about to post is how to specify a relative path for my xsd file. When I deploy this to my web hosting server - it won't find it because it is defined using my dev machine path. Any thoughts?
MikeD
A: 

Not sure what you mean: ASP.Net C# code always runs on the web server, NEVER on the client side. The two just don't mix.


Update based on your comment:

How are you declaring your "script"? There are two ways to do it:

  • A single <script runat="server"> //code here </script> element. This can handle any of the page events, including page load.

  • Inline script elements/bee-stings. <% %> tags interspersed in your markup. It is a common misconsception that code in those tags runs on page load. As you may have gathered from my use of hte word "misconception", this is wrong. Code in those tags does not run until much later in the page lifecycle (PreRender, IIRC).

Note that 2nd item especially, as this code runs after the databinding phase is already finished. If you want to use that dataset as the datasource for the control, you'll need to DataBind() it manually.

Joel Coehoorn
Sorry I meant that I was not using code behind but rather defining my page load in script within my aspx file.
MikeD
I am using the first way.
MikeD