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 Managemen开发者_如何学编程t 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);
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
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.
精彩评论