Solved

Connecting text box to SQL Field on website.

Posted on 2008-10-09
4
1,177 Views
Last Modified: 2012-05-05
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
Comment
Question by:StrategicGnomer
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:crumber
ID: 22683659
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
 

Author Comment

by:StrategicGnomer
ID: 22688309
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
 

Accepted Solution

by:
StrategicGnomer earned 0 total points
ID: 22691186
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
 
LVL 1

Expert Comment

by:crumber
ID: 22703730
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

809 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