473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Filling textboxes from database - ASP.NET

23 New Member
Hello, all. I am building an ASP application where we have a table named Clients and I am wanting to display a specific client record on a form with textboxes. In other words, I want to have the first name in one box, middle name in another, last name in another, you get the idea. I will also need to be able to update these records, as well as add new records using these textboxes. All I have tried so far has failed. Can someone give me some guidance in this task? Any assistance will be greatly appreciated. Thanks.
Apr 1 '08 #1
20 1977
jeffstl
432 Recognized Expert Contributor
Hello, all. I am building an ASP application where we have a table named Clients and I am wanting to display a specific client record on a form with textboxes. In other words, I want to have the first name in one box, middle name in another, last name in another, you get the idea. I will also need to be able to update these records, as well as add new records using these textboxes. All I have tried so far has failed. Can someone give me some guidance in this task? Any assistance will be greatly appreciated. Thanks.
You will need to do 4 main things once your HTML form is built.

1) Set up a connection to your database
http://www.connectionstrings.com

2) Read that database via an SQL script to load the data into variables on your page.
http://www.w3schools.com/sql/sql_select.asp

3) Set the form objects values equal to the variables you set from the database
4) On the save you will need to re-get the user entered data from the forms via the request.form syntax and then use SQL Update to update your record in the database
http://www.w3schools.com/sql/sql_update.asp

To do all this you will also need to learn about ASP in general and how to use its syntax to accomplish things on the actual page
http://www.w3schools.com/asp/asp_intro.asp
Apr 1 '08 #2
soonerpgh
23 New Member
At the moment I have a SqlDataSource configured and the form is built, of course. Wouldn't the SqlDataSource take care of the connection?
Apr 1 '08 #3
jeffstl
432 Recognized Expert Contributor
At the moment I have a SqlDataSource configured and the form is built, of course. Wouldn't the SqlDataSource take care of the connection?
Does that mean you are using ASP.NET ?

The direction I gave you was specifically for Classic ASP.

If you are using .NET its a whole new ball game for what you will need to do.
Apr 1 '08 #4
soonerpgh
23 New Member
Yes, I am using ASP.Net. Sorry I didn't specify that before. I just assumed you all could read my mind. My apologies.
Apr 1 '08 #5
jeffstl
432 Recognized Expert Contributor
Yes, I am using ASP.Net. Sorry I didn't specify that before. I just assumed you all could read my mind. My apologies.
OK.

Please post the specific errors or problems and the relevant code and I will take a look.

If you need a place to start though with .NET I would take a look at the tutorials here.

http://www.w3schools.com/aspnet/aspnet_intro.asp
Apr 1 '08 #6
soonerpgh
23 New Member
There isn't much to it, actually, because I really don't know where to start. Everything I find on databinding is referring to the data controls. Anyway, here is my test code:
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <script runat="server">
  6.  
  7. </script>
  8.  
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head runat="server">
  11.     <title>Test Page</title>
  12. </head>
  13. <body>
  14.     <form id="form1" runat="server">
  15.     <div>
  16.  
  17.         &nbsp;<asp:Label ID="Label1" runat="server" Text="First Name:"></asp:Label>
  18. &nbsp;
  19.  
  20.         <asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
  21.         <br />
  22.         <br />
  23.         <asp:Button ID="btnSave" runat="server" CommandName="Update" Text="Save" />
  24.         <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  25.             ConnectionString="<%$ ConnectionStrings:FUND235v2.0aConnectionString %>" 
  26.  
  27.             SelectCommand="SELECT FirstName FROM [tbl_Client] WHERE ([ClientID] = @ClientID)" 
  28.             UpdateCommand="UPDATE tbl_Client SET ([FirstName} = @FirstName) WHERE ([ClientID] =  @ClientID) ">
  29.             <SelectParameters>
  30.                 <asp:ControlParameter ControlID="HiddenField1" Name="ClientID" 
  31.                     PropertyName="Value" />
  32.             </SelectParameters>
  33.             <UpdateParameters>
  34.                 <asp:Parameter Name="ClientID" />
  35.             </UpdateParameters>
  36.         </asp:SqlDataSource>
  37.  
  38.         <asp:HiddenField ID="HiddenField1" runat="server"  Value="7"/>
  39.  
  40.     </div>
  41.     </form>
  42. </body>
  43. </html>
Apr 1 '08 #7
jeffstl
432 Recognized Expert Contributor
OK the methods you are using may work but they are not what I am used to.

For example I don't see a Sub Page_Load on your page, which is the first event that will fire as the page loads.

Also you placed your text box out there and gave it an ID of txtFirstName, but you did not bind that to anything.

To be honest I am not familiar with the way you are doing it, using only asp tags without any code-behind.

However, if you put something like this in your script area it should get your started. Keep in mind you will also need to figure out your connection string for this.

Expand|Select|Wrap|Line Numbers
  1.  Sub Page_Load
  2.      dim DBCommand,strsql,DBReader
  3.     ConnectDB()
  4.      if not page.ispostback
  5.           '  SET SQL
  6.             strsql = "Select * from Clients"
  7.             DBCommand = New OleDbCommand(strsql, DBConnection)
  8.             DBReader = DBCommand.ExecuteReader()
  9.             DBReader.Read()
  10.  
  11.             txtFirstName.Text = DBReader("first_name")
  12.      end if
  13.      CloseDB()
  14. End Sub
  15.  
Also, if you want here is an example of how I set up my database connections, except I use MS Access right now....

Expand|Select|Wrap|Line Numbers
  1. 'SUB DATABASE CONNECTIONS
  2. sub ConnectDB
  3.   DBConnection = New OleDbConnection( _
  4.     "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  5.     "Data Source=" & Request.ServerVariables("APPL_PHYSICAL_PATH") & "..\db\Mydata.mdb")
  6.   DBConnection.Open()
  7. end Sub
  8.  
  9. sub CloseDB
  10.   DBConnection.Close()
  11. end sub
  12.  
Then I can just place ConnectDB and CloseDB wherever I need to in my page

Hopefully this helps you out.
Apr 2 '08 #8
soonerpgh
23 New Member
Thanks for the suggestion. I have the conncetion string worked out, that isn't a problem.

As far as the "code behind" statement you made, I have VS set to put all the code in one file. I can still use VB, it is just aplied in the script area as you stated. I will look closely at what you did here and attempt to implement the idea and see where it takes me. Thanks again.
Apr 3 '08 #9
jeffstl
432 Recognized Expert Contributor
Thanks for the suggestion. I have the conncetion string worked out, that isn't a problem.

As far as the "code behind" statement you made, I have VS set to put all the code in one file. I can still use VB, it is just aplied in the script area as you stated. I will look closely at what you did here and attempt to implement the idea and see where it takes me. Thanks again.

Ah yes. I sometimes forget about VS. I use hand coding and dreamweaver to do almost everything.

Yes you can store you code-behind in a .vb file, sorry.
Apr 3 '08 #10
waterfall
22 New Member
is ur problem solved?
Apr 3 '08 #11
soonerpgh
23 New Member
is ur problem solved?
Nope, still trying to get that going.
Apr 7 '08 #12
soonerpgh
23 New Member
A new twist on this one... can someone show me how to simply pull the data, say from a data grid to load into variables? If I can accomplish that, the rest is a piece of cake.
Apr 8 '08 #13
jeffstl
432 Recognized Expert Contributor
A new twist on this one... can someone show me how to simply pull the data, say from a data grid to load into variables? If I can accomplish that, the rest is a piece of cake.
Currently (still learning asp.net a little) I only know of 2 ways you can do this.

One is to use the OnItemCommand property of a Data grid. This event is called\fired whenever a ButtonColumn is clicked on your data grid (Example: A delete button)

Expand|Select|Wrap|Line Numbers
  1. 'first part is code-behind for the event when the button link is clicked
  2. 'second 'part is the actual data grid
  3.  
  4. Sub ClientDataCommands(sender as Object, InvGrid As DataGridCommandEventArgs)
  5.      dim MyClientNameVar as TableCell = ClientData.Item.Cells(1)
  6.      'now you have the client name from the row that was clicked by the user
  7.      'loaded into MyClientNameVar
  8. End Sub
  9.  
  10.  
  11.     <asp:DataGrid id="ClientData" runat="server"
  12.     AutoGenerateColumns="False" CellPadding="0"
  13.     HeaderStyle-BackColor="#A4A6CF"
  14.     HeaderStyle-ForeColor="000033"
  15.     HeaderStyle-Font-Size="12"
  16.     HeaderStyle-HorizontalAlign="Left"
  17.     HeaderStyle-Font-Bold="True"
  18.     BackColor="#eeeeee" Width="27%"
  19.     HorizontalAlign="Left"
  20.     Font-Bold="True" Font-Name="Tahoma"
  21.     Font-Size="8pt"
  22.     OnItemCommand="ClientDataCommands">        
  23.     <Columns>
  24.           <asp:BoundColumn HeaderText="Delete." ItemStyle-BackColor="#FFFFFF" ItemStyle-ForeColor="#990000" />
  25.           <asp:ButtonColumn DataTextField="client_name"   
  26.   </Columns>
  27.   </asp:DataGrid>
  28.  
  29.  
The other way is to simply use the same exact SQL you used to populate your grid and read the data from the reader instead of binding it to a data grid control

Keep in mind this code here is assuming you will only be returning 1 record from your SQL query. If you have multiple you will have to deal with that somehow
Expand|Select|Wrap|Line Numbers
  1.   strsql = "SELECT * FROM ClientTable where ClientID = " & ClientID & ""
  2.   DBCommand = New OleDbCommand(strsql, DBConnection)
  3.   DBReader = DBCommand.ExecuteReader()
  4.   If DBReader.Read then
  5.        MyClientNameVar = DBReader("client_name")
  6.        'now you can continue to load all columns from the table
  7.   End While
  8.   DBReader.Close
  9.  
Apr 8 '08 #14
soonerpgh
23 New Member
OK, I am getting really annoyed with myself on this, as I seem to be wasting the time of others as well as my own with my incorrect terminology.

To correct my prior post, I am using a Gridview control, not a Datagrid. Please forgive my mistake. By the way, that last solution looked promising, maybe now that I have the right info out here, you guys can either kick me to the gutter, if you wish, or maybe some smart individual will hit on the solution. I know somwone, somewhere has done this. I just need to find that person and pick his/her brain.

Again, sorry for the confusion.
Apr 9 '08 #15
jeffstl
432 Recognized Expert Contributor
OK, I am getting really annoyed with myself on this, as I seem to be wasting the time of others as well as my own with my incorrect terminology.

To correct my prior post, I am using a Gridview control, not a Datagrid. Please forgive my mistake. By the way, that last solution looked promising, maybe now that I have the right info out here, you guys can either kick me to the gutter, if you wish, or maybe some smart individual will hit on the solution. I know somwone, somewhere has done this. I just need to find that person and pick his/her brain.

Again, sorry for the confusion.
Don't beat yourself up
Programming can be picky that way. Syntax and terminology being exact I mean ;-)

The only thing that matters is you now know the difference so you won't make that mistake again. Each little step is an extra bit of knowledge.

Unfortunately I myself have not worked with gridviews yet as I find the datagrid to be a suitable control for my needs.

Here is a Microsoft tutorial for gridviews though.

http://quickstarts.asp.net/QuickStar.../gridview.aspx
Apr 9 '08 #16
soonerpgh
23 New Member
OK, it appears that the Gridview is a bust. So let's forget that for now. How about pulling specific data, i.e., FirstName, out of the SQLDataSource? Anyone know how to do that?
Apr 10 '08 #17
Curtis Rutland
3,256 Recognized Expert Specialist
OK, it appears that the Gridview is a bust. So let's forget that for now. How about pulling specific data, i.e., FirstName, out of the SQLDataSource? Anyone know how to do that?
There are really two directions you can go. One involves doing all the code behind and forgetting about your SqlDataSource. The other involves using a GridView.

If you want to do it in the code behind, you need to pick an event, such as a button click. If you are using Visual Studio, double click the button in Design View to auto-generate the method for the event. Then you will need to use either an OdbcConnection, OleDbConnection, or SqlConnection, depending on the type of connection you are using (if you are using mssql server, use SqlConnection, otherwise use Ole or Odbc.) I'll assume you are using OleDb. Here's the code:

Codebehind page:

Expand|Select|Wrap|Line Numbers
  1. Partial Class _Default
  2.     Inherits System.Web.UI.Page
  3.  
  4.     Protected Sub bUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bUpdate.Click
  5.         Dim conn As New System.Data.OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("connectionString").ConnectionString)
  6.         Dim sql As String
  7.         sql = String.Format("UPDATE tbl_Client SET [FirstName] = '{0}' WHERE [ClientID] = {1}", tbFirstName.Text, tbClientId.Text)
  8.         Dim cmd As New System.Data.OleDb.OleDbCommand(sql, conn)
  9.         conn.Open()
  10.         cmd.ExecuteNonQuery()
  11.         conn.Close()
  12.     End Sub
  13. End Class
  14.  
Aspx page:
[HTML]
<asp:TextBox ID="tbClientId" runat="server"></asp:TextBox>
<asp:TextBox ID="tbFirstName" runat="server"></asp:TextBox>
<asp:Button ID="bUpdate" runat="server" Text="Update" />
[/HTML]

What happens here is when you click the button, you create new database connection and command objects, create a sql update statement using the values from the textboxes, and executes the commands. You won't see anything but a page refresh, but look at your db, it will change.

Remember to replace the ("connectionString") with whatever your connection string is named in the web.config file.

Using a similar method, you could populate the text boxes with a (OleDb/Odbc/Sql)DataAdapter and a DataSet. If you need me to, I can post that method as well.
Apr 10 '08 #18
Curtis Rutland
3,256 Recognized Expert Specialist
I just realized that I had answered the half of the question you hadn't asked. Anyway, I went ahead and made up a full example for you. There are two methods used, and I think you should be able to figure it out from there. I wrote up a little tutorial too. It's really too big to post, so I'll just link it:
Here
And I promise that this isn't blogspam. I don't have a single ad on the site and am gaining nothing by you going there. I have the writeup and the example solution hosted there. (It's actually my unfinished blog that I am trying to code from scratch).

By the way, if this is against the rules, just let me know and I won't do it again. It's just that I've leeched a lot of help off this site, and now I want to give something back.
Apr 11 '08 #19
soonerpgh
23 New Member
Thanks a lot. I haven't had the opportunity to check this one out yet, but I appreciate your answer. I will check it as soon as I get the chance, hopefully this afternoon, or tomorrow.
Apr 14 '08 #20
soonerpgh
23 New Member
Here's the question I have about this one. I want it to auto fill upon loading the page, so can I use the page load event instead of the button? It would be ridiculous to require the user to click a button every time a new page comes up in order to fill that page with data.

If I cannot, how would I use the gridview?
Apr 18 '08 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
1
by: Eva | last post by:
Hi I have 2 comboboxes on my form that gathers 2 parameters: The Caravan_Name and the caravan_Length. these 2 values that the user selects are placed into a stored procedure as parameters. The...
1
by: Dave | last post by:
Hello, I've been waisting tons of time on this, so I'll just ask and hopefully someone will be able to help. I am opening an excel document and placing values into it's "letterhead" section...
3
by: Omar Llanos | last post by:
I have Form1 and Form2 (which is inherited from Form1), and I created a button in Form2 that will fill up a textbox in Form1. What code would do that? I tried the simplest way: //from child...
1
by: Dan | last post by:
Hi, I have a requirement where in I need to automatically fill a web form and trigger events on the web form automatically. Firstly can this be done. For example: I have a site say xyz.com which...
3
by: Jensen bredal | last post by:
Hello, I need to write different kind of forms that the customer can use to enter data and save the data into database. (The data access will be hand coded) My problem is that the form may vary...
5
by: \A_Michigan_User\ | last post by:
I'm using asp.net/vb.net/ado.net to create a very simple user interface. (Everything works if I use STATICALLY created textBoxes... but not when I make them DYNAMICALLY.) 1. Execute a SQL...
1
by: sakee123 | last post by:
hi.. i have a html form in which i have three textboxes and a submit button.. after filling three textboxes in a form the submit button should be enabled. prior to which it should be disabled.....
1
by: TheNewBoy | last post by:
Hi All. I am new to this forum... Don't know how to start but here it goes.... I am writing in VB.net a program with one form, yet mulitple textboxes. the data appears in each text box as...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.