Solved

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

Posted on 2016-08-12
11
49 Views
Last Modified: 2016-08-16
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
Comment
Question by:misnstt
  • 5
  • 5
11 Comments
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41754575
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
 

Author Comment

by:misnstt
ID: 41754788
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
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41754973
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
 

Author Comment

by:misnstt
ID: 41755155
Hello good evening  unfortunately that change had no effect and the same problem persist.
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41755186
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 41755212
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
 

Author Comment

by:misnstt
ID: 41755451
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
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41755583
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
 

Author Comment

by:misnstt
ID: 41758278
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
 
LVL 12

Accepted Solution

by:
funwithdotnet earned 500 total points
ID: 41758414
This doesn't look correct:
(UserId = UserId)">

Open in new window


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

Open in new window

0
 

Author Closing Comment

by:misnstt
ID: 41758424
Thanks for your assistance.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now