Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 80
  • Last Modified:

Select User Name and Display Detailed User Information on seperate page in ASP.NET

I have a ASP.NET page with a Details View which displays the currently logged in users Name.    I want the user to click the Name from the Details view and open the users detailed information.    Currently upon selecting the user name in the view page the result is that the correct UserId is in the URL of the Detailed information page however the result is not the correct user.   Basiclly what the query result is the first record of the database.    
I am attaching images of the URL and the actual  record that appears in the Detailed Information page.     I need help please in getting the correct record to show in the detailed information page.

Thanks
Record ReturnedURL

MAIN PAGE WITH DETAILD VIEW++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<asp:DetailsView AutoGenerateRows="False" DataSourceID="dsUserDetails" ID="DetailsView1" runat="server"  GridLines="None" BorderStyle="None">
                                <Fields>
                                       
                                    <asp:TemplateField  SortExpression="UserName">
                                     
                                        <ItemTemplate>
                                       
                             
                                   
                                        <asp:HyperLink ID="HyperLink4" runat="server" Font-Underline="false"
                                        NavigateUrl='<%# String.Format("ChefProfile.aspx?UserId={0}", Eval("UserId"))%>'
                                       Text='<%# Bind(" LastName")%>'>
                                    </asp:HyperLink>
                                    </li>
                                         
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                       </Fields>
                                <HeaderStyle />
                            </asp:DetailsView>




DETAILED INFORMATION PAGE+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<asp:FormView ID="formview" runat="server" DataKeyNames="UserId" DataSourceID="profile1" OnDataBound="formview_DataBound">
       
        <ItemTemplate>
              <div id="profile_info">
                  <asp:Label ID="UserId" runat="server" Text='<%# Bind("UserId") %>'></asp:Label>
                    <h2 id="name" class="transition-02"><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("FirstName") %>' />
            <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("LastName") %>' /></h2>
                    <h4 class="line"><asp:Label ID="OccupationLbl" runat="server" Text='<%# Bind("Occupation") %>'></asp:Label></h4>
       <h6><span class="fa fa-map-marker"></span>
           <asp:Label ID="Citylbl" runat="server" Text='<%# Bind("City") %>'></asp:Label>, <asp:Label ID="lblstate" runat="server" Text='<%# Bind("State") %>'></asp:Label></h6>
                   </div>          
                     
               
        </ItemTemplate>
        </asp:FormView>

<asp:SqlDataSource ID="profile1" runat="server" ConnectionString="<%$ ConnectionStrings:dbMyCMSConnectionString %>" SelectCommand="SELECT UserId, FirstName, LastName, PersonalWebsite, City, State, PostalCode, DayPhone, CellPhone, Twitter, Facebook, Pinterest, busemail, googleplus, groups, charge, Gender, Occupation, ProfileImage, languages FROM aspnet_CustomProfile"></asp:SqlDataSource>
0
misnstt
Asked:
misnstt
  • 5
  • 5
1 Solution
 
funwithdotnetCommented:
Can you post the code for this page?

You already know that the detail view is binding to the first record. All we need to do is follow the data binding and see why it is grabbing the first record. Obviously the data source isn't getting the message about the user id.
0
 
misnsttAuthor Commented:
DETAILED INFORMATION PAGE+++++++=

CODE BEHIND=
 protected void Page_Load(object sender, EventArgs e)
    {

       
        if (!Page.IsPostBack)
        {

            string userId = Membership.GetUser().ProviderUserKey.ToString();

            formview.DataBind();
          UserId.Text = DataBinder.Eval(formview.DataItem, "UserId").ToString();

        }
    }

    protected void formview_DataBound(object sender, EventArgs e)
    {
        UserId.Text = DataBinder.Eval(formview.DataItem, "UserId").ToString();
    }

 <div class="col-lg-12 col-md-12 col-sm-9 col-xs-12">



                                            <!-- Profile info -->
                                         
<asp:FormView ID="formview" runat="server" DataKeyNames="UserId" DataSourceID="profile1" OnDataBound="formview_DataBound">
       
        <ItemTemplate>
              <div id="profile_info">
                  <asp:Label ID="UserId" runat="server" Text='<%# Bind("UserId") %>'></asp:Label>
                    <h2 id="name" class="transition-02"><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("FirstName") %>' />
            <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("LastName") %>' /></h2>
                    <h4 class="line"><asp:Label ID="OccupationLbl" runat="server" Text='<%# Bind("Occupation") %>'></asp:Label></h4>
       <h6><span class="fa fa-map-marker"></span>
           <asp:Label ID="Citylbl" runat="server" Text='<%# Bind("City") %>'></asp:Label>, <asp:Label ID="lblstate" runat="server" Text='<%# Bind("State") %>'></asp:Label></h6>
                   </div>          
                     
               
        </ItemTemplate>
        </asp:FormView>
     
         <asp:SqlDataSource ID="profile1" runat="server" ConnectionString="<%$ ConnectionStrings:dbMyCMSConnectionString %>" SelectCommand="SELECT UserId, FirstName, LastName, PersonalWebsite, City, State, PostalCode, DayPhone, CellPhone, Twitter, Facebook, Pinterest, busemail, googleplus, groups, charge, Gender, Occupation, ProfileImage, languages FROM aspnet_CustomProfile"></asp:SqlDataSource>                                
                                            <!-- End Profile info -->  


                                            <!-- Profile Description -->
                                            <div id="profile_desc">
                                                <p>
                                                    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac risus nibh. Donec adipiscing luctus tur
                                                </p>
                                                <p>
                                                    Lorem ipsum dolor sit amet, consectetur adipiscing eli
                                                </p>
                                            </div>

MAIN PAGE WITH DETAIL VIEW+++++++++++++++++++++++++++++++++++

<div class="top-bar main-bg no-border">
                        <div class="container">
                              <asp:LoginView runat="server" ViewStateMode="Disabled">
                        <AnonymousTemplate>

                  <ul class="top-info f-left">
                        <li><a href="login.aspx">Chef Register <i class="fa fa-user after"></i></a></li>
                        <li><a href="login.aspx">Log in <i class="fa fa-lock after"></i></a></li>
                        
                    </ul>
                  

                        </AnonymousTemplate>
                        <LoggedInTemplate>
                            <ul class="top-info f-left">
                                  
                                  
                                  <li class="dropdown language-selector">
                                          <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-close-others="true"><i class="fa fa-user"></i><asp:LoginName ID="LoginName2" runat="server" /></a>
                                          <ul class="dropdown-menu">
                               
                               
<li>
   <asp:DetailsView AutoGenerateRows="False" DataSourceID="dsUserDetails" ID="DetailsView1" runat="server"  GridLines="None" BorderStyle="None">
                                <Fields>
                                       
                                    <asp:TemplateField  SortExpression="UserName">
                                     
                                        <ItemTemplate>
                                       
                             
                                    <li>
                                        <asp:HyperLink ID="HyperLink4" runat="server" Font-Underline="false"
                                        NavigateUrl='<%# String.Format("ChefProfile.aspx?UserId={0}", Eval("UserId"))%>'
                                       Text='<%# Bind("LastName")%>'>
                                    </asp:HyperLink>
                                    </li>
                                         
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                       </Fields>
                                <HeaderStyle />
                            </asp:DetailsView>
   
</li>




                                    <li>
                                     <a href="ChefProfile.aspx" title=""><div style="text-align: center; font-size: medium">My Profile</div></a></li>
                                    <li>
                                    <a href="ChefDashboard.aspx" title=""><div style="text-align: center; font-size: medium">Edit Profile</div></a></li>
                        <li>
                              <a href="Change password.aspx" title=""><div style="text-align: center; font-size: medium">Change Password</div></a></li>
                          <li>
                              <a href="Logout.aspx" title=""><div style="text-align: center; font-size: medium">Logout</div></a></li>
                                          </ul>
                                    </li>
                            </ul>
                         
      </LoggedInTemplate>
                    </asp:LoginView>
0
 
funwithdotnetCommented:
Try removing the NOT IsPostBack conditional.

///if (!Page.IsPostBack)
        ///{

            string userId = Membership.GetUser().ProviderUserKey.ToString();

            formview.DataBind();
          UserId.Text = DataBinder.Eval(formview.DataItem, "UserId").ToString();

        ///}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
misnsttAuthor Commented:
Hello good evening  unfortunately that change had no effect and the same problem persist.
0
 
funwithdotnetCommented:
Okay, where does dsUserDetails come from? That appears to be the source of the detail view.

Somewhere, the postback has to tell the the app which userid  to lookup to display the correct record in the details view. That is apparently not happening.

Sorry, I'm not more help. I tend to do everything in code.
0
 
David Johnson, CD, MVPOwnerCommented:
SelectCommand="SELECT UserId, FirstName, LastName, PersonalWebsite, City, State, PostalCode, DayPhone, CellPhone, Twitter, Facebook, Pinterest, busemail, googleplus, groups, charge, Gender, Occupation, ProfileImage, languages FROM aspnet_CustomProfile">

This will return all records but you only are displaying 1 record, you need to add a WHERE clause

SELECT UserId, FirstName, LastName, PersonalWebsite, City, State, PostalCode, DayPhone, CellPhone, Twitter, Facebook, Pinterest, busemail, googleplus, groups, charge, Gender, Occupation, ProfileImage, languages FROM aspnet_CustomProfile WHERE Userid = " + useridvalue
0
 
misnsttAuthor Commented:
Good morning I added the Where clause however the outcome is the same.  

On the first page I have the Details View and it is attached to the aspnet_CustomProfile table.

<asp:DetailsView AutoGenerateRows="False" DataSourceID="dsUserDetails" ID="DetailsView1" runat="server"  GridLines="None" BorderStyle="None">
                                <Fields>
                                       
                                    <asp:TemplateField  SortExpression="UserName">
                                     
                                        <ItemTemplate>
                                       
                             
                                    <li>
                                        <asp:HyperLink ID="HyperLink4" runat="server" Font-Underline="false"
                                        NavigateUrl='<%# String.Format("ChefProfile.aspx?UserId={0}", Eval("UserId"))%>'
                                       Text='<%# Bind("LastName")%>'>
                                    </asp:HyperLink>
                                    </li>
                                         
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                       </Fields>
                                <HeaderStyle />
                            </asp:DetailsView>

When the link is selected on the first page it goes to the details page and it is still retrieving the first record of the table.   I am using the same table on the Details page .   Here is the FormView and SqlDatasource on the Details page.

<asp:FormView ID="formview" runat="server" DataKeyNames="UserId" DataSourceID="profile1" OnDataBound="formview_DataBound">
       
        <ItemTemplate>
              <div id="profile_info">
                  <asp:Label ID="UserId" runat="server" Text='<%# Bind("UserId") %>'></asp:Label>
                    <h2 id="name" class="transition-02"><asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("FirstName") %>' />
            <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("LastName") %>' /></h2>
                    <h4 class="line"><asp:Label ID="OccupationLbl" runat="server" Text='<%# Bind("Occupation") %>'></asp:Label></h4>
       <h6><span class="fa fa-map-marker"></span>
           <asp:Label ID="Citylbl" runat="server" Text='<%# Bind("City") %>'></asp:Label>, <asp:Label ID="lblstate" runat="server" Text='<%# Bind("State") %>'></asp:Label></h6>                                    
                   </div>          
                     
               
        </ItemTemplate>
        </asp:FormView>
     
         <asp:SqlDataSource ID="profile1" runat="server" ConnectionString="<%$ ConnectionStrings:dbMyCMSConnectionString %>"
             SelectCommand="SELECT UserId, FirstName, LastName, PersonalWebsite, City, State, PostalCode, DayPhone, CellPhone, Twitter, Facebook, Pinterest, busemail, googleplus, groups, charge, Gender, Occupation, ProfileImage, languages FROM aspnet_CustomProfile WHERE (UserId = UserId)">
         </asp:SqlDataSource>    

Here is the Behind Code of the Details Page:

 protected void Page_Load(object sender, EventArgs e)
    {
       
        CreateDate.Text = DateTime.Now.ToString();
     ///   if (!Page.IsPostBack)
     ///   {

            string userId = Membership.GetUser().ProviderUserKey.ToString();
        profile1.DataBind();
            formview1.DataBind();
            formview.DataBind();
      //    UserId.Text = DataBinder.Eval(formview.DataItem, "UserId").ToString();
         

      ///  }
    }


The address on the Details page returns the correct UserId :
http://localhost:54060/ChefProfile.aspx?UserId=73884da9-240e-4cfb-b885-87b1b0fc273a

However the record being returned in the Formview is not the correct UserId  it is returning the first record of the table.

Thanks for your continued assistance.
0
 
funwithdotnetCommented:
pseudocode for detail page:
if (isPostback) ...
/// check the UserId querystring
string pid= Request.QueryString["UserId"];
if (pid.Length > 0){
/// create a dataview filter
string filter = "UserId='" + pid + "'";
/// create a new dataview with the desired record.
DataView UserDv = new DataView(profile1, filter, "LastName",  DataViewRowState.OriginalRows);
DetailsView1.DataSource = UserDv;
DetailsView1.DataBind();
}
}

Open in new window


I think something like that is what you need
0
 
misnsttAuthor Commented:
I changed the SQLDatasource on the Details page and added the SelectParameters  to:

 <asp:SqlDataSource ID="profile1" runat="server" ConnectionString="<%$ ConnectionStrings:dbMyCMSConnectionString %>"
             SelectCommand="SELECT UserId, FirstName, LastName, PersonalWebsite, City, State, PostalCode, DayPhone, CellPhone, Twitter, Facebook, Pinterest, busemail, googleplus, instagram, groups, charge, Gender, Occupation, ProfileImage, languages FROM aspnet_CustomProfile WHERE (UserId = UserId)">
          <SelectParameters>
           <asp:QueryStringParameter Name="UserId" QueryStringField="UserId" Type="int32"  />
            </SelectParameters>
         </asp:SqlDataSource>    


Unfortunately I am getting the following error message now:


Input string was not in a correct format.
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:



Line 18:        string userId = Membership.GetUser().ProviderUserKey.ToString();
Line 19:         profile1.DataBind();
Line 20:        formview1.DataBind();
Line 21:         formview.DataBind();
Line 22:        
 

 Source File:  c:\Users\Laptop\Documents\Visual Studio 2015\WebSites\MyWSAT35\ChefProfile.aspx.cs    Line:  20

Stack Trace:



[FormatException: Input string was not in a correct format.]
   System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +11783513
   System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +120
   System.String.System.IConvertible.ToInt32(IFormatProvider provider) +46
   System.Convert.ChangeType(Object value, TypeCode typeCode, IFormatProvider provider) +297
   System.Web.UI.WebControls.Parameter.GetValue(Object value, String defaultValue, TypeCode type, Boolean convertEmptyStringToNull, Boolean ignoreNullableTypeChanges) +126
   System.Web.UI.WebControls.Parameter.GetValue(Object value, Boolean ignoreNullableTypeChanges) +63
   System.Web.UI.WebControls.Parameter.get_ParameterValue() +41
   System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) +247
   System.Web.UI.WebControls.SqlDataSourceView.InitializeParameters(DbCommand command, ParameterCollection parameters, IDictionary exclusionList) +257
   System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +588
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +22
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   System.Web.UI.WebControls.FormView.DataBind() +9
   ChefProfile.Page_Load(Object sender, EventArgs e) in c:\Users\Laptop\Documents\Visual Studio 2015\WebSites\MyWSAT35\ChefProfile.aspx.cs:20
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952
0
 
funwithdotnetCommented:
This doesn't look correct:
(UserId = UserId)">

Open in new window


Perhaps this will help?
(UserId = @UserId)">

Open in new window

0
 
misnsttAuthor Commented:
Thanks for your assistance.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now