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 1848
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 year...
how would I populate the 'year' field and then submit it for processing?
I have no control of the server... just the client script.
<form action="cgi-bin/leapyear.py">
<p>Enter a year and find out if it's a leap year:
<input type="text"...
|
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 specific websites would be
appreciated!
thanks in advance!!
|
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 populate multiple
webpages containing customer information. There isn't a one-to-one
correlation between the stored procedure and a webpage. In other
words, a webpage may have to refer to 1 or more DataTables to populate
itself. Therefore, a...
|
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 controls on that page, before
transferring focus to the browser-page and showing it to the user? (the
user would be responsible for submitting the page.)
|
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. here is the javascript produced:
<script>window.opener.document.forms.txtEndDate.value =
'7/15/2006';self.close()</script>
I basically need to populate txtEndDate on the content page.
if
| |
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 e.g. consider the following code:
<script runat="server">
Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
'create an array of colors
|
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 As New SqlConnection(".....")
Public Function GetAddress(ByVal UserID As Integer) As
SqlDataReader
Dim sqlCmd As SqlCommand
Dim sqlReader As SqlDataReader
|
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 to be
able
to automatically populate a "generic" schedule for a few weeks or
more
at a time.
The schedule table contains:
(pk) schedule_id, start_datetime, end_datetime, shift_employee,
|
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 to be
able
to automatically populate a "generic" schedule for a few weeks or
more
at a time.
The schedule table contains:
(pk) schedule_id, start_datetime, end_datetime, shift_employee,
|
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
Fields Names: countrycode, make
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |