Solved

Connecting text box to SQL Field on website.

Posted on 2008-10-09
4
1,171 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
dynamic menu in asp.net c# 11 29
Change part of a string 2 23
Different Delete Messages 7 17
SQL Server 2012 Express to Full 5 0
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

757 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

21 Experts available now in Live!

Get 1:1 Help Now