Solved

Connecting text box to SQL Field on website.

Posted on 2008-10-09
4
1,182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Viewers will learn how the fundamental information of how to create a table.

622 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