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
3 1840
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: - Dim oFieldName() As String
-
'Make sure you populate this array with your fieldnames - you'll need to
-
'specify an array limit; you could also modify the array and set the
-
'values to the control indices of the textboxes to be updated.
-
-
Dim oDR As SqlDataReader = oCmd.ExecuteReader()
-
If oDR.HasRows Then
-
While oDR.Read
-
For i As Integer = 0 To oRdr.FieldCount - 1
-
Me.Controls(FieldName(i)).Text = oRdr(i)
-
Next
-
End While
-
End If
-
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. - Dim oCon As New SqlConnection("Driver=....")
-
Dim sSQL As String = "Select Fields From Table..."
-
Dim oDA As New SqlDataAdapter(sSQL, oCon)
-
Dim oDT As New DataTable
-
oDA.Fill(oDT)
-
Dim i As Integer = 0 'Row we wish to extract data from
-
If oDT.Rows.Count > 0 Then
-
For Each oCol As DataColumn In oDT.Columns
-
Me.Controls(oCol.ColumnName).Text = oDT.Rows(i).Item(oCol.Ordinal).ToString
-
Next
-
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.
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
- Private sCon As String = "Server=.\SQL2005DEV;Database=Dummy;Trusted_Connection=yes;"
-
Private sSelect As String = "Select KeyField, Data1, Data2, Data3 From Dummy Where KeyField = 239"
-
Private oCon As SqlClient.SqlConnection
-
Private oDA As SqlClient.SqlDataAdapter
-
Private oQB As SqlClient.SqlCommandBuilder
-
Private oDT As DataTable
-
Private _CurrentRec As Integer = -1
-
-
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
-
oCon = New SqlConnection(sCon)
-
oDA = New SqlDataAdapter(sSelect, oCon)
-
oQB = New SqlCommandBuilder(oDA)
-
oDT = New DataTable("Person")
-
oDT.Columns.Add("KeyField", GetType(Integer))
-
oDT.Columns.Add("Data1", GetType(String))
-
oDT.Columns.Add("Data2", GetType(String))
-
oDT.Columns.Add("Data3", GetType(String))
-
-
Dim oKeys(0) As DataColumn
-
oKeys(0) = oDT.Columns("KeyField")
-
oDT.PrimaryKey = oKeys
-
-
oDA.Fill(oDT)
-
-
If oDT.Rows.Count > 0 Then
-
_CurrentRec = oDT.Rows(0).Item(0)
-
TextBox1.Text = oDT.Rows(0).Item(1).ToString
-
TextBox2.Text = oDT.Rows(0).Item(2).ToString
-
TextBox3.Text = oDT.Rows(0).Item(3).ToString
-
End If
-
-
End Sub
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
-
Dim oRow As DataRow = oDT.Rows.Find(_CurrentRec)
-
oRow.Item(1) = TextBox1.Text
-
oRow.Item(2) = TextBox2.Text
-
oRow.Item(3) = TextBox3.Text
-
oDA.Update(oDT)
-
-
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...
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |