473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Populate Form from SQL Server

8
Apologies if this has already been posted.

I am coding in VB

I am trying to find the best way to populate a form from a sql Database in .net 2005

I have a company table that has > 20 columns, I will be selecting a specific row. I have populated the form with textboxes that are named so that they reflect the column names of the company table.

I want to select the row, populate the form by looping through the columns.

I do not want to use a datagrid. I will also want to update the database with any changes.

Any help will be Appreciated
Mar 19 '08 #1
3 1840
balabaster
797 Expert 512MB
Apologies if this has already been posted.

I am coding in VB

I am trying to find the best way to populate a form from a sql Database in .net 2005

I have a company table that has > 20 columns, I will be selecting a specific row. I have populated the form with textboxes that are named so that they reflect the column names of the company table.

I want to select the row, populate the form by looping through the columns.

I do not want to use a datagrid. I will also want to update the database with any changes.

Any help will be Appreciated
I'm assuming you're using the SqlConnection/SqlCommand to query the database. I'm not sure if there's a way to obtain a field name from the DataReader class, and there's only two overloads for accessing a field, either by index or by name. So tying the field from your DataReader with a textbox may not be as straightforward as you would like:
Expand|Select|Wrap|Line Numbers
  1. Dim oFieldName() As String
  2. 'Make sure you populate this array with your fieldnames - you'll need to
  3. 'specify an array limit; you could also modify the array and set the
  4. 'values to the control indices of the textboxes to be updated.
  5.  
  6. Dim oDR As SqlDataReader = oCmd.ExecuteReader()
  7. If oDR.HasRows Then
  8.   While oDR.Read
  9.     For i As Integer = 0 To oRdr.FieldCount - 1
  10.       Me.Controls(FieldName(i)).Text = oRdr(i)
  11.     Next
  12.   End While
  13. End If
  14. oCmd.Dispose()
An alternative approach may be to use an SqlDataAdapter to populate a DataTable. You can then run a For-Each on the DataColumns within the table and reference the ColumnName. This won't perform quite as well as the DataReader, but will provide you with more flexibility.
Expand|Select|Wrap|Line Numbers
  1. Dim oCon As New SqlConnection("Driver=....")
  2. Dim sSQL As String = "Select Fields From Table..."
  3. Dim oDA As New SqlDataAdapter(sSQL, oCon)
  4. Dim oDT As New DataTable
  5. oDA.Fill(oDT)
  6. Dim i As Integer = 0 'Row we wish to extract data from
  7. If oDT.Rows.Count > 0 Then
  8.   For Each oCol As DataColumn In oDT.Columns
  9.     Me.Controls(oCol.ColumnName).Text = oDT.Rows(i).Item(oCol.Ordinal).ToString
  10.   Next
  11. End If
For the auto-update, you'd have to set the autopostback on each of the textboxes. I would recommend using an AJAX extender on them so that the update on the database is run in the background giving a more fluid user experience.
Mar 19 '08 #2
MIkeC
8
Thanks for the Rapid reply.

I have a couple of problems. It seems that Autopostback is an ASP feature I am coding in VB. sorry If this is wrong

The form will only display 1 row which is controlled by the Where clause. Ideally I will have a button on the form that will perform the Update code and close the form. I was looking for an update command that will commit all changes back to database

Thanks in anticipation

Mike
Mar 20 '08 #3
balabaster
797 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. Private sCon As String = "Server=.\SQL2005DEV;Database=Dummy;Trusted_Connection=yes;"
  2. Private sSelect As String = "Select KeyField, Data1, Data2, Data3 From Dummy Where KeyField = 239"
  3. Private oCon As SqlClient.SqlConnection
  4. Private oDA As SqlClient.SqlDataAdapter
  5. Private oQB As SqlClient.SqlCommandBuilder
  6. Private oDT As DataTable
  7. Private _CurrentRec As Integer = -1
  8.  
  9. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  10.  
  11.   oCon = New SqlConnection(sCon)
  12.   oDA = New SqlDataAdapter(sSelect, oCon)
  13.   oQB = New SqlCommandBuilder(oDA)
  14.   oDT = New DataTable("Person")
  15.   oDT.Columns.Add("KeyField", GetType(Integer))
  16.   oDT.Columns.Add("Data1", GetType(String))
  17.   oDT.Columns.Add("Data2", GetType(String))
  18.   oDT.Columns.Add("Data3", GetType(String))
  19.  
  20.   Dim oKeys(0) As DataColumn
  21.   oKeys(0) = oDT.Columns("KeyField")
  22.   oDT.PrimaryKey = oKeys
  23.  
  24.   oDA.Fill(oDT)
  25.  
  26.   If oDT.Rows.Count > 0 Then
  27.     _CurrentRec = oDT.Rows(0).Item(0)
  28.     TextBox1.Text = oDT.Rows(0).Item(1).ToString
  29.     TextBox2.Text = oDT.Rows(0).Item(2).ToString
  30.     TextBox3.Text = oDT.Rows(0).Item(3).ToString
  31.   End If
  32.  
  33. End Sub
  34.  
  35. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  36.  
  37.   Dim oRow As DataRow = oDT.Rows.Find(_CurrentRec)
  38.   oRow.Item(1) = TextBox1.Text
  39.   oRow.Item(2) = TextBox2.Text
  40.   oRow.Item(3) = TextBox3.Text
  41.   oDA.Update(oDT)
  42.  
  43. End Sub
This isn't quite what you were looking for but it's close enough that you should be able to figure out where to go from there...
Mar 20 '08 #4

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

Similar topics

5
by: rbt | last post by:
How can I make a python client script connect to a Web server and automatically populate form fields and then submit the form? For example, say I wanted to check and see if 1924 was a leap...
1
by: picarama | last post by:
hi i created a form in outlook 2000 hoping that i can populate with data stored in access 2000. however, i don't know the steps to populate the outlook form!? any advise or direction to...
3
by: Yul | last post by:
Hi, We are in the process of designing an ASP.NET app, where a user will enter some 'Customer ID' to be queried in the database. If the ID is valid, several stored procedures will be called to...
5
by: John A Grandy | last post by:
is it possible to write a vb.net code (intended to run on the client-side) that would invoke an instance of IE, have it download a page from a certain URL, and then pre-populate some of the...
4
by: Mori | last post by:
I am using masterPage and I need to populate a textbox that is in a content control with data from popup page that is not part of the master page. This code works if no masterpage is involved. ...
5
by: Arpan | last post by:
In order to populate any server control with data dynamically, is it ALWAYS NECESSARY to either BIND the DataSource to that server control or call the DataBind method of that server control? For...
2
by: rn5a | last post by:
This function in a VB class file takes UserID as a parameter & returns a SqlDataReader to the calling function which exists in a ASPX page: Namespace NConnect Public Class Cart Private sqlConn...
4
by: Nate | last post by:
I am looking to populate a Schedule table with information from two other tables. I am able to populate it row by row, but I have created tables that should provide all necessary information for me...
2
by: Nate | last post by:
I am looking to populate a Schedule table with information from two other tables. I am able to populate it row by row, but I have created tables that should provide all necessary information for me...
5
by: giandeo | last post by:
Hello Experts. Could you find a solution for this problem please! I have the following tables in Access Database Table Name: origin Fields Names: country, countrycode Table Name: make...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.