473,663 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Populate Form from SQL Server

8 New Member
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 1848
balabaster
797 Recognized Expert Contributor
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 New Member
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 Recognized Expert Contributor
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
9073
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"...
1
1596
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!!
3
2689
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...
5
1891
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.)
4
3000
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
5
1863
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
2
1874
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
4
6568
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,
2
2038
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,
5
3997
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
0
8436
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, 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...
0
8345
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,...
0
8858
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, 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...
0
8771
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 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...
0
8634
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 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...
1
6186
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 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...
0
5657
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();...
0
4182
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...
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.