Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1187
  • Last Modified:

Connecting text box to SQL Field on website.

So I want customers to be able to sign up online on my site. So I made a textbox but I'm not sure how to tie that to a field in a SQL Server database. I'm using mainly VS 08 to build the site but I do know a bit about just putting in the code so either way would work. Thanks.
0
StrategicGnomer
Asked:
StrategicGnomer
  • 2
  • 2
1 Solution
 
crumberCommented:
It really depends as there are many ways to do this.  
I like to use the Formview control where I tie a sqldatasource control to the formview.   The sqldatasource control would contain your select, insert, update or delete statement in order to handle the sql accordingly.
In regards to simply sending and retrieving information via a textbox, you would tie the sqldatasource with a select statement that contains the datafield that you want to use.
When creating the formview, you would place the textbox in one of the templates (Insert, Edit) and tie it to the datafield in the sqldatasource by updating the databinding for the Textboxes Text attribute to that field and entering Bind("datafieldname") for two-way databinding or Eval("datafieldname") for one way databinding or just to retrieve the information.
When wanting two-way databinding, the Bind function is used and automatically will retrieve and send new data for that field when the formview is inserted or updated.
Again, you need to have the update properties defined in your sqldatasource in order to use two-way databinding.
Hope that helps.  Again, I am assuming you have some knowledge of asp.net
0
 
StrategicGnomerAuthor Commented:
I have a bit of knowledge but as it shows, I'm a beginner. I do kinda understand what you're saying however, but by two way you're saying it'll pull data and be able to edit it correct? Now, what about just dumping data onto the database? Or would I just use Bind? And how would I make it so that it only adds a new record and not shows existing records? Because I want people to sign up, not see other applicants you know?

Again, basic knowledge, if I see the code, I can mostly figure it out but I'm in no shape to be creating code on my own just yet.

So let me just see, I have this code, it pulls the data from the SQL Server, and a textbox. Again, beginner here, so how would I make this so that, customer puts in their first name, let's say, Bob, and it adds a new record in the database with Bob?
<td height="365" class="style15">
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApartmentDBConnectionString %>" 
        SelectCommand="SELECT [FirstName] FROM [tblApartmentData]">
    </asp:SqlDataSource>
                    </td>
<td height="365" class="style2">
    <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
                    </td>

Open in new window

0
 
StrategicGnomerAuthor Commented:
Partial Class Default2
    Inherits System.Web.UI.Page
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        SqlDataSource1.InsertParameters("NameFirst").DefaultValue = txtName.Text.ToString()
        SqlDataSource1.InsertParameters("NameLast").DefaultValue = txtDescription.Text.ToString()
        SqlDataSource1.Insert()
    End Sub
End Class
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<table style="width: 426px">
<tr>
<td>
Category Name:</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
<td>
</td>
</tr>
<tr>
<td>
Description:&nbsp;</td>
<td>
<asp:TextBox ID="txtDescription" runat="server"></asp:TextBox></td>
<td>
&nbsp;<asp:Button ID="Button2" runat="server" Text="Add" /></td>
</tr>
</table>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NewSignupDBConnectionString %>"
SelectCommand="SELECT [NameFirst], [NameLast], [MidInit], [Address1], [Address2], [City], [State], [ZipCode], [DateofBirth], [SSN], [HomePh], [WorkPh], [AltPh], [BillAddress], [BillAddress2], [BillCity], [BillState], [BillZip], [Email], [ApartmentComplex] FROM [tblNewCust]"
InsertCommand="INSERT INTO [tblNewCust] ([NameFirst], [NameLast]) VALUES (@NameFirst, @NameLast)"
>
 
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Width="426px">
<Columns>
<asp:BoundField DataField="NameFirst" HeaderText="NameFirst" 
        SortExpression="NameFirst" />
<asp:BoundField DataField="NameLast" HeaderText="NameLast" 
        SortExpression="NameLast" />
<asp:BoundField DataField="MidInit" HeaderText="MidInit" SortExpression="MidInit" />
    <asp:BoundField DataField="Address1" HeaderText="Address1" 
        SortExpression="Address1" />
    <asp:BoundField DataField="Address2" HeaderText="Address2" 
        SortExpression="Address2" />
    <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
    <asp:BoundField DataField="ZipCode" HeaderText="ZipCode" 
        SortExpression="ZipCode" />
    <asp:BoundField DataField="DateofBirth" HeaderText="DateofBirth" 
        SortExpression="DateofBirth" />
    <asp:BoundField DataField="SSN" HeaderText="SSN" SortExpression="SSN" />
    <asp:BoundField DataField="HomePh" HeaderText="HomePh" 
        SortExpression="HomePh" />
    <asp:BoundField DataField="WorkPh" HeaderText="WorkPh" 
        SortExpression="WorkPh" />
    <asp:BoundField DataField="AltPh" HeaderText="AltPh" SortExpression="AltPh" />
    <asp:BoundField DataField="BillAddress" HeaderText="BillAddress" 
        SortExpression="BillAddress" />
    <asp:BoundField DataField="BillAddress2" HeaderText="BillAddress2" 
        SortExpression="BillAddress2" />
    <asp:BoundField DataField="BillCity" HeaderText="BillCity" 
        SortExpression="BillCity" />
    <asp:BoundField DataField="BillState" HeaderText="BillState" 
        SortExpression="BillState" />
    <asp:BoundField DataField="BillZip" HeaderText="BillZip" 
        SortExpression="BillZip" />
    <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
    <asp:BoundField DataField="ApartmentComplex" HeaderText="ApartmentComplex" 
        SortExpression="ApartmentComplex" />
</Columns>
</asp:GridView>
    </div>
    </form>
</body>
</html>

Open in new window

0
 
crumberCommented:
If you want to just have users add data to the database but not have them view the data, you can control that by limiting your formview control to only allow insertmode.
That way, you would continue to use the Bind function for two-way databinding; however, the user would never see the data or Editmode of the formview.  
You can accomplish this by either using a formview and having it take care of all the parameter assignment or by code.
Let me know if this helps.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now