Link to home
Start Free TrialLog in
Avatar of Sheritlw
SheritlwFlag for United States of America

asked on

Binding Linq to SQL to Fields in Wizard Control

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
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

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?
Avatar of Sheritlw

ASKER

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

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.
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.

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
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
For VB.NET, this is the site that I use:

101 Visual Basic LINQ Samples
http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx
Great, Thank you so much for your help.
You have helped me a lot over the passed couple of years.

Thanks again