Solved

Binding Linq to SQL to Fields in Wizard Control

Posted on 2010-09-20
12
480 Views
Last Modified: 2013-11-11
Hi,
I have used a wizard control to create a setup form that can be edited.  
I also have a linq to sql data source but cannot figure out how to bind the text boxes, drop downs etc. to the appropriate field in the data source.
How do I bind the controls?
Thanks
0
Comment
Question by:Sheritlw
  • 6
  • 6
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33726602
I am not sure that I completely understand your question.  Can you show me the HTML for the Wizard Control?  What elements did you define in the LINQ-to-SQL data context?
0
 

Author Comment

by:Sheritlw
ID: 33727145
There seems to be no data binding option within the wizard.
I tried to bind within the load event of each wizard step, but can't get the syntax to work (I included the code in the code section).

I have tried many examples but cannot get them to work... below is my latest attempt
 'Dim q = From a In dc.Stylists() Where a.UserID = Session("UserID")

The q will not give me any fields in my datacontext.

Thanks
THE WIZARD



<asp:Wizard ID="wzdSetup" runat="server" 

     DisplaySideBar="true" CssClass="wizardsetup"

     ActiveStepIndex="0" BorderWidth="0"  > 

     

     

     <SideBarStyle CssClass="wizardsidebar"  

         VerticalAlign="Top" HorizontalAlign="Center" />

     <SideBarButtonStyle CssClass="wizardhyper" />

         

            <HeaderStyle CssClass="wizardheader" HorizontalAlign="Center" />

               

            <HeaderTemplate>



            <b><%=wzdSetup.ActiveStep.Title%></b>



            <br />



            </HeaderTemplate>

            

            <StartNavigationTemplate>





            <asp:Button ID="NextButton" runat="server"  Text="NEXT" cssClass="wizardnextback" CommandName="MoveNext" />

               

            </StartNavigationTemplate>



            <StepNavigationTemplate >



            <asp:Button ID="PreviousButton" runat="server" Text="PREVIOUS" CommandName="MovePrevious" />



            <asp:Button ID="StartNextButton" cssClass="wizardnextback" runat="server" Text="NEXT" CommandName="MoveNext" />           

               

            </StepNavigationTemplate>



            <FinishNavigationTemplate>



            



            <asp:Button ID="PreviousButton" runat="server" Text="PREVIOUS" CommandName="MovePrevious" />



            <asp:Button ID="FinishButton" runat="server" Text="FINISH" cssClass="wizardnextback" CommandName="MoveComplete" />      



            </FinishNavigationTemplate>



            <WizardSteps>



                <asp:WizardStep ID="wzdContactInfo" runat="server" Title="Contact Info.">

               <table class="wizardtable">

               

                    <colgroup>

                         <col width="35%" />

                         <col width="75%" />

                     </colgroup>     

                         

                         <tr>

                              <th class="logintext" colspan="2">

                                   Enter Contact Information</th>

                         </tr>

                         <tr>

                              <td colspan="2">

                                   &nbsp;

                              </td>

                         </tr>

                         <tr>

                              <td>

                                   First Name:</td>

                              <td>

                                

                                   <asp:TextBox ID="txtFirstName" runat="server" CssClass="wizardboxes"

                                        MaxLength="50" ></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

                                        ControlToValidate="FirstName" Display="Dynamic" 

                                        ErrorMessage="First name is required."></asp:RequiredFieldValidator></td></tr><tr>

                              <td>

                                   Last Name:</td><td>

                                   <asp:TextBox ID="txtLastName" runat="server" CssClass="wizardboxes" 

                                        MaxLength="50"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 

                                        ControlToValidate="LastName" Display="Dynamic" 

                                        ErrorMessage="Last name is required."></asp:RequiredFieldValidator></td></tr><tr>

                              <td>

                                   Professional Category:</td><td>

                                   <asp:DropDownList ID="ddProfCategory" runat="server" CssClass="wizardboxes" 

                                        Width="72%">

                                        <asp:ListItem>What is your mother&#39;s maiden name?</asp:ListItem><asp:ListItem>In what city were you born?</asp:ListItem><asp:ListItem>What is your favorite sport?</asp:ListItem><asp:ListItem>How much wood could a woodchuck chuck if a woodchuck could chuck wood?</asp:ListItem></asp:DropDownList></td></tr><tr>

                              <td>

                                   Salon Name:</td><td>

                                   <asp:TextBox ID="txtSalonName" runat="server" Columns="10" 

                                        CssClass="wizardboxes" MaxLength="10"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 

                                        ControlToValidate="SalonName" Display="Dynamic" 

                                        ErrorMessage="Salon name is required in order for clients to search."></asp:RequiredFieldValidator></td></tr><tr>

                              <td>

                                   Address:</td><td>

                                   <asp:TextBox ID="txtSalonAdd" runat="server" CssClass="wizardboxes" 

                                        MaxLength="50"></asp:TextBox></td></tr><tr>

                              <td>

                                   City/Province:</td><td>

                                   <asp:TextBox ID="txtSalonCity" runat="server" CssClass="wizardboxes" 

                                        MaxLength="50"></asp:TextBox></td></tr><tr>

                              <td>

                                   State/Postal:</td><td>

                                   <asp:DropDownList ID="ddStateID" runat="server" CssClass="wizardboxes" 

                                        Width="45%">

                                   </asp:DropDownList>

                                   <asp:TextBox ID="SalonZip" runat="server" MaxLength="10" Width="23%"></asp:TextBox></td></tr><tr>

                              <td>

                                   Email:</td><td>

                                   <asp:TextBox ID="txtStylistEmail" runat="server" CssClass="wizardboxes"></asp:TextBox><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 

                                        ControlToValidate="txtStylistEmail" Display="Dynamic" 

                                        ErrorMessage="You must enter a valid email address" 

                                        ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></td></tr><tr>

                              <td>

                                   Domain Name:</td><td>

                                   <asp:TextBox ID="txtDomain" runat="server" CssClass="wizardboxes" 

                                        MaxLength="20"></asp:TextBox><asp:Label ID="lblDomain" runat="server" Text="www.smsmain.com/domainname"></asp:Label></td></tr><tr>

                              <td colspan="2">

                                   &nbsp;

                              </td>

                         </tr>

                   

              

              

          </table>

          

          <table class="wizardtable">

               

                    <colgroup>

                         <col width="25%" />

                         <col width="25%" />

                         <col width="25%" />

                         <col width="25%" />

                    </colgroup>

                      

                         <tr>

                              <td>

                                   Primary Phone</td><td>

                                   Work Phone</td><td>

                                   Mobile Phone</td><td>

                                   Home Phone</td></tr><tr>

                              <td>

                                   <asp:TextBox ID="txtStylistPrimePhone" runat="server" MaxLength="10" 

                                        Width="80%"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" 

                                        ControlToValidate="txtStylistPrimePhone" Display="Dynamic" 

                                        ErrorMessage="Primary phone number is required."></asp:RequiredFieldValidator></td><td>

                                   <asp:TextBox ID="txtStylistWkPhone" runat="server" MaxLength="10" Width="80%"></asp:TextBox></td><td>

                                   <asp:TextBox ID="txtStylistMobPhone" runat="server" MaxLength="10" Width="80%"></asp:TextBox></td><td>

                                   <asp:TextBox ID="txtStylistHomePhone" runat="server" MaxLength="10" Width="80%"></asp:TextBox></td></tr><tr>

                              <td colspan="4">

                                   &nbsp;

                              </td>

                         </tr>

                         <tr>

                              <td colspan="4">

                                   <asp:CheckBox ID="chkStylistSearchable" runat="server" Checked="True" 

                                        Text="Allow your name to be found by potentials clients/clients?" />

                              </td>

                         </tr>

                         <tr>

                              <td colspan="4">

                                   <asp:CheckBox ID="chkAllowBioView" runat="server" Checked="True" 

                                        Text="Allow potentials clients/clients to view your Bio?" />

                              </td>

                         </tr>

                         <tr>

                              <td colspan="4">

                                   <asp:CheckBox ID="chkAllowPort" runat="server" Checked="True" 

                                        Text="Allow potentials clients/clients to view your portfolio?" />

                              </td>

                         </tr>

                         <tr>

                              <td colspan="4">

                                   &nbsp;

                              </td>

                         </tr>

                         <tr>

                              <td align="center" colspan="4">

                                   <asp:Button ID="buttContactCancel" runat="server" CssClass="wizardbutton" 

                                        Text="Cancel" />

                                   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

                                   <asp:Button ID="buttContactSave" runat="server" CssClass="wizardbutton" 

                                        OnClick="buttContactSave_Click" Text="Save" />

                              </td>

                         </tr>

                         <tr>

                              <td colspan="4">

                                   &nbsp;

                                   

                                   <asp:LinqDataSource ID="LinqStylistContact" runat="server" 

                                        ContextTypeName="SalonClassesDataContext" 

                                        Select="new (StylistID, UserID, ProfCategoryID, SalonName, SalonAdd, SalonCity, StateID, SalonZip, FullName, FName, LName, PrimaryPhone, WkPhone, MobPhone, HmPhone, EmailAdd, Searchable, AllowBioView, AllowPortView, DomainName)" 

                                        TableName="Stylists" Where="UserID == @UserID">

                                        <WhereParameters>

                                             <asp:SessionParameter DbType="Guid" Name="UserID" SessionField="UserID" />

                                        </WhereParameters>

                                   </asp:LinqDataSource>

                                   

                              </td>

                         </tr>

                    

             

               </table>                

           </asp:WizardStep>



THE STEP LOAD EVENT



Protected Sub wzdContactInfo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles wzdContactInfo.Load



          Dim dc As New SalonClassesDataContext()

          If Not Page.IsPostBack Then

               If User.Identity.IsAuthenticated Then

                    Dim u As MembershipUser = Membership.GetUser(User.Identity.Name.ToString())

                    Session("UserID") = u.ProviderUserKey.ToString()

               End If

          End If



          'Dim q = From a In dc.Stylists() Where a.UserID = Session("UserID")





          'Me.txtAdd1.Text = 









     End Sub

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33727758
There isn't any data-binding for the Wizard control, that I know of, but you should be able to get references to the underlying controls with FindControl, and then set the values, or fill a DropDownList, etc.
0
 

Author Comment

by:Sheritlw
ID: 33727928
I have figured out how assign information to the fields and I can manage it in code behind.
My problem now is the linq to sql syntax I am using to retrieve the field values from the db.

Dim dc As New SalonClassesDataContext()

Dim q = From a In dc.Stylists() Where a.UserID = Session("UserID")

From what I have read, I should be able to get the values from the db from the line above.

But q does not return the fieldname.

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33727943
I don't see any Select condition, like this (guess):

Dim q = From a In dc.Stylists() Where a.UserID = Session("UserID") Select a.FieldName
0
 

Author Comment

by:Sheritlw
ID: 33727962
Oh,  so it in not selecting the table?  I need to create a select statement?
What if I wanted to just get the table?
Thanks
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 33728143
If you want all the properties, then you can do something like this:

Dim q = From a In dc.Stylists() Where a.UserID = Session("UserID") Select a
0
 

Author Comment

by:Sheritlw
ID: 33728275
That's cool... Loving Linq to sql

But how do I assign the values to the text boxes?

Example..

Dim q = From a In dc.Stylists() Where a.UserID = Session("UserID") Select a
       
  Me.txtAdd1.Text = q??????

Thanks
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 33728437
If you are assigning to TextBox controls, then you need a single record.  You can use FirstOrDefault or SingleOrDefault to get a single record.

Dim q = (From a In dc.Stylists() Where a.UserID = Session("UserID") Select a).FirstOrDefault()

If q Is Nothing Then
    Throw New NullReferenceException("Stylist not found for user ID  " & Session("UserID"))
End If

Me.txtAdd1.Text = q.Address1
0
 

Author Comment

by:Sheritlw
ID: 33728525
That was it.  I have read a lot of stuff about LInq to SQL, but never read anything on firstordefault.
Is there a good site that has examples of linq to sql statements?

Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33729128
For VB.NET, this is the site that I use:

101 Visual Basic LINQ Samples
http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx
0
 

Author Comment

by:Sheritlw
ID: 33729140
Great, Thank you so much for your help.
You have helped me a lot over the passed couple of years.

Thanks again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

9 Experts available now in Live!

Get 1:1 Help Now