locked
Bind populated label to database field. RRS feed

  • Question

  • User-1361977450 posted

    I'm using the below code to populate a label inside my insert template with the logged in users' name...

    <asp:Label runat="server" Text="<%# Membership.GetUser().UserName %>" id="Label1">
           </asp:Label>

    Now that my label gets the UserName how can I also bind this label to the field "name" in my database when the user submits? 

     

    Thanks!

     

     

     

    Here is my AccessDataSource with the Insert stuff.

     

     

    <asp:AccessDataSource runat="server" ID="AccessDataSource2" InsertCommand="INSERT INTO [ads] ([headline], [description], [price], [picture], [tags], [name], [website], [email], [phone1], [phone2], [note], [category], [display], [spam], [date], [company], [size], [color]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" DataFile="../../access_db/oops.mdb">
          <InsertParameters>
           <asp:parameter Name="headline" Type="String" />
           <asp:parameter Name="description" Type="String" />
           <asp:parameter Name="price" Type="String" />
           <asp:parameter Name="picture" Type="String" />
           <asp:parameter Name="tags" Type="String" />
           <asp:parameter Name="name" Type="String" />
           <asp:parameter Name="website" Type="String" />
           <asp:parameter Name="email" Type="String" />
           <asp:parameter Name="phone1" Type="String" />
           <asp:parameter Name="phone2" Type="String" />
           <asp:parameter Name="note" Type="String" />
           <asp:parameter Name="category" Type="String" />
           <asp:parameter Name="display" Type="String" />
           <asp:parameter Name="spam" Type="String" />
           <asp:parameter Name="date" Type="String" />
           <asp:parameter Name="company" Type="String" />
           <asp:parameter Name="size" Type="String" />
           <asp:parameter Name="color" Type="String" />
          </InsertParameters>
         </asp:AccessDataSource>

     

     

    Monday, May 10, 2010 10:02 AM

All replies

  • User-1199946673 posted

    Are you sure yoy want to use the UserName. Ussualy, I use the UserID (Membership.GetUser().ProviderUserKey to link a record to a user. In that case, you can use a custom parameter:

    http://weblogs.asp.net/anasghanem/archive/2008/10/15/creating-custom-parameters-for-your-data-sources.aspx

    By the way, you declare all the inserparameters to be a String, but I expect some parameters (like price, date, size) do have another datatype...

    And you're using am accesdatasource, meaning you specify a datafile. If I look to your datafile property, I expect your database to be outside the root of your website, which is fine. But when you need (or want) to put the database in another location (for example in a production environment) you need to alter all datafile property's in all your pages. A better option is to use an SQLDatasource instead, which allows you to store the connectionstring in web.config, so you'll need to alter the connectionstring in one place only:

    http://www.mikesdotnetting.com/Article/78/AccessDataSource-SqlDataSource-and-connecting-to-Access-databases-in-ASP.NET

    Monday, May 10, 2010 2:57 PM
  • User-1361977450 posted

    Thanks for the tips hans_v.  I appreciate your help.

    I've converted my pages to use a SQLDatasource. 

    Now on to the custom parameter ...and then messing with the insertparameter datatypes.

    Cheers!

     

    Monday, May 10, 2010 5:43 PM