Solved

Binding Linq to SQL to Fields in Wizard Control

Posted on 2010-09-20
12
473 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to go to Web page 2 46
Exception in Log4Net 1 17
Form design in vb.net 7 17
Loop through Multiple Processes Async 2 15
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

11 Experts available now in Live!

Get 1:1 Help Now