sqldatasource where UserId=

Last post 05-14-2008 5:16 AM by dhimant. 3 replies.

Sort Posts:

  • sqldatasource where UserId=

    05-13-2008, 7:41 PM
    • Loading...
    • Geek-O
    • Joined on 04-25-2008, 6:36 AM
    • Posts 29

     I am trying to configure a sqldatasource control using the where clause to grab a single record with a certain UserId. Teh UserId is stored in a variable of type System.Gui and I cant seem to figure out how to do this. Can someone give me a hand please?

  • Re: sqldatasource where UserId=

    05-13-2008, 8:02 PM
    • Loading...
    • anas
    • Joined on 09-21-2006, 8:31 AM
    • Jerusalem
    • Posts 4,054

    I'm assuming that your working with membership , and you want to show the Data related to current user ,

    you can  add a where clause to the SqlDataSource  Select Command in Selecting event :

     

        Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
            SqlDataSource1.SelectCommand += String.Format("  WHERE (UserID = {0})", Membership.GetUser().ProviderUserKey.ToString())
        End Sub
     
    Best Regards,

    Anas Ghanem| LogFile
  • Re: sqldatasource where UserId=

    05-13-2008, 10:38 PM
    • Loading...
    • Geek-O
    • Joined on 04-25-2008, 6:36 AM
    • Posts 29

    Thanks for your help. I am trying to display some information from a different table and a different user. The code you provided helped a lot, but it still is not loading the data into the detailsview control. Here is what i have:

    <asp:DetailsView ID="DetailsViewPersonal" runat="server" Height="50px"
                    Width="125px" BackColor="White" BorderColor="White"
                    BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
                    DataSourceID="SqlDataSourcePersonal" GridLines="None">
                    <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                    <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                    <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
                    <EditRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                </asp:DetailsView>

     

    <asp:SqlDataSource ID="SqlDataSourcePersonal" runat="server"
            ConnectionString="<%$ ConnectionStrings:ModelsFinderConnectionString %>"
            SelectCommand="SELECT [Age], [DOB], [Gender], [ChestSize], [CupSize], [Waist], [Height], [Hips], [Weight], [HairColor] FROM [Personal_Stats]"
            onselecting="SqlDataSourcePersonal_Selecting">
        </asp:SqlDataSource>

     protected void SqlDataSourcePersonal_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            SqlDataSourcePersonal.SelectCommand += String.Format("  WHERE (UserId = {0})", ThisUserId.ToString());
        }

    the detailsview shows the headers on the left but does not fill the column on the right with the information. What am i doing wrong?

  • Re: sqldatasource where UserId=

    05-14-2008, 5:16 AM
    Answer
    • Loading...
    • dhimant
    • Joined on 09-19-2007, 7:13 AM
    • India
    • Posts 771

    Geek-O:
     protected void SqlDataSourcePersonal_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            SqlDataSourcePersonal.SelectCommand += String.Format("  WHERE (UserId = {0})", ThisUserId.ToString());
        }
     

    I don't think this is the way how you should be doing this.

    First you need to change the query in your select command directly in the SQL data source declaration:

    Geek-O:
    SelectCommand="SELECT [Age], [DOB], [Gender], [ChestSize], [CupSize], [Waist], [Height], [Hips], [Weight], [HairColor] FROM [Personal_Stats]"
     

    SelectCommand="SELECT [Age], [DOB], [Gender], [ChestSize], [CupSize], [Waist], [Height], [Hips], [Weight], [HairColor] FROM [Personal_Stats]  where UserId = @UserId"

    Then you need to declare this parameter in the select parameter's collection for the data source.

    <SelectParameters>
                        <asp:Parameter Name="UserId" Type="String" />
    </SelectParameters>

    Then in the data source selecting event modify your code like:

      protected void SqlDataSourcePersonal_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.Parameters["@UserId"].Value = ThisUserId.ToString();
        }

    There are of course other ways of using the parameters in your data source commands ( select, insert, update, delete ) for that you may refer to the MSDN.

    Thanks.
    Dhimant Trivedi
    "When the going gets tough, the tough get going."

    PS: Be sure to "Mark as Answer" the post which answered your question.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter