Friday, March 9, 2012

InsertParameters for SqlDataSource

Hello, I have a SqlDataSource named "OrderHistoryDataSource" that I want to add a parameter to from the code behind file. THe reason I want to do this is todynamicly add the UserID from a currently logged on user. What am I doing wrong? Is there a better way?My code below:

---------------------Code behind file-----------------------

using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partialclass OrderHistory : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e) {//If the user is not logged in this will redirect to Login.aspxif (!Request.IsAuthenticated) { Response.Redirect("~/Login.aspx"); }//test to display userID MembershipUser myObject = Membership.GetUser();string UserGUID = myObject.ProviderUserKey.ToString(); SqlDataSource DataSource = Page.FindControl("OrderHistoryDataSource"); DataSource.InsertParameters.Add("CurrentUserId", UserGUID.ToString()); DataSource.Select(); }}

---------------------Asp.netfile-----------------------

<%@. Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="OrderHistory.aspx.cs" Inherits="OrderHistory" Title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="CONTENT" Runat="Server"> <table id="ContentTable"> <tr> <td id="ContentTitleCell" colspan="3" style="width: 684px"> ORDER HISTORY</td> </tr> <tr> <td colspan="3" rowspan="2" style="width: 684px"> <asp:GridView ID="OrderHistoryGrid" runat="server"> </asp:GridView>   <asp:SqlDataSource ID="OrderHistoryDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString%>" SelectCommand="SELECT * FROM [UserOrders] WHERE ([UserID] = [CurrentUserID]) ORDER BY [OrderID] VALUES (@.UserId, @.Address, @.City, @.State, @.Zip)" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName%>"> <InsertParameters></InsertParameters> </asp:SqlDataSource> </td> </tr> <tr> </tr> </table></asp:Content>

Hello,

By looking in the code that you have given, I can definitely say there is some thing wrong. Please check youe code again.

You can follow the code below.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="MiddleName" HeaderText="MiddleName" SortExpression="MiddleName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="OrderHistoryDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [UserID], [Title], [FirstName], [MiddleName], [LastName] FROM [UserOrders] WHERE ([UserID] = @.UserID)">
<SelectParameters>
<asp:Parameter Name="UserID" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

---------------------Codebehindfile-----------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partialclass OrderHistory : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//If the user is not logged in this will redirect to Login.aspx
if (!Request.IsAuthenticated)
{
Response.Redirect("~/Login.aspx");
}

//test to display userID
MembershipUser myObject = Membership.GetUser();
string UserGUID = myObject.ProviderUserKey.ToString();
OrderHistoryDataSource.SelectParameters["UserID"].DefaultValue = UserGUID;
GridView1.DataBind();
}
}

Thanks,

Deepesh Verma


|||

Thank you very much. I figured I had the general idea; I was just off by a bit.

|||

I would typically suggest moving the code to the SqlDatasource_Selecting event. Within that event, you can set any parameters you want through the e.Command property. It also will get executed each time a select will fire, and only when a select will fire.

No comments:

Post a Comment