Monday, March 12, 2012

retrieving data from sqldataprovider w/out presentation control

Is there a way to retrieve a returned value from a stored procedure w/out binding do a data presentationi control? I have the following sqldataprovider...

<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="f_GetUser" ConnectionString="<%$ ConnectionStrings:MyServer %>" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:formparameter name="username" formfield="txtusername" /> <asp:formparameter name="password" formfield="txtpassword" /> </SelectParameters> </asp:SqlDataSource>

Do I have to make a gridview to get the value that is returned from this stored procedure? The SP only returns one value (a count of records) and I want to set it to a variable. Is there a way, similar to a recordset in ASP, that I can access that one value?

here is my stored procedure...

CREATE PROCEDURE f_GetUser @.usernamevarchar(50),@.passwordvarchar(50)ASSELECTCOUNT(*)AS numusersFROM f_UsersWHERE username=@.usernameAND password=@.password

I just want to retrieve "numusers" and use it in a conditional...

tia for your help

Here is a way to display data as a table using code to access the returned data

<%@.PageLanguage="C#" %>

<!

DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

scriptrunat="server">

protectedvoid Page_Load(object sender,EventArgs e)

{

System.Data.DataView dv = (System.Data.DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

System.Data.DataTable dt = dv.Table;

foreach (System.Data.DataRow drin dt.Rows)

{

TableRow tr =newTableRow();

t.Rows.Add(tr);

foreach (System.Data.DataColumn dcin dt.Columns)

{

TableCell td =newTableCell();

tr.Cells.Add(td);

td.Text = dr[dc.ColumnName].ToString();

}

}

}

</

script>

<

htmlxmlns="http://www.w3.org/1999/xhtml">

<

headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<asp:Tablerunat="server"ID="t">

</asp:Table>

<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

SelectCommand="SELECT [EmployeeID], [LastName], [FirstName] FROM [Employees]"></asp:SqlDataSource>

</form>

</body>

</html>

|||

thanks sb! You don't happen to have a vb version of that same code do you? >< I suck at C# :)

|||

Sorry, I just put this together off the peg. You shuld be able to easily write a VB version line by line as there are no special work rounds

I use C# most of the time, but if customers want VB, then fine, I don't mind using both - I can sit on the fence and see the sense of both!

No comments:

Post a Comment