13 Replies - 4542 Views - Last Post: 12 January 2012 - 01:30 AM Rate Topic: -----

#1 mpe-IT  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 08:04 PM

i create a db and it has 1 table with 40 records on it. in one form i create 40 textbox and what i wanted to do is to retrieve each record in every textbox. what should i add in my code?

Imports System.Data.SqlClient
Public Class frmIpAddress


    Dim ipcon As New SqlConnection
    Dim ipcmd As New SqlCommand
    Dim ipdr As SqlDataReader
    Private Sub frmIpAddress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim constring2 As String = "Data Source=HPMINI110\SQLEXPRESS;Initial Catalog=EDPC_DB;" & _
                          " User=sa;Password=12321;Integrated Security=false"
        ipcon.ConnectionString = constring2
        ipcon.Open()
        ipcmd.Connection = ipcon
        ipcmd.CommandText = "select * from LAN where ID=1"
        ipdr = ipcmd.ExecuteReader
        While ipdr.Read() 'While Data is Present

            TextBox1.Text = ipdr("IpAddress").ToString 
            
        End While
        ipdr.Close()
        ipcon.Close()
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve records SQL w/ VB.net

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,255
  • Joined: 12-June 08

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 08:10 PM

Is your data coming back?

Why not just have one datagrid and be done with it?

You would have to use the me.controls(<string name of control>) - cast as a text box - to do this.. that would require making the string name of up your textboxes.. probably combining "Textbox" with a value you would increment.
Was This Post Helpful? 1
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 08:25 PM

So you have 40 text boxes on your form, and you wish to set the value of each of them?
You could have a For Loop to go throughout the fields in the database, look through the control collection and cast each result as a TextBox, then set the value of its Text property?
If not, please clarify.
Was This Post Helpful? 0
  • +
  • -

#4 mpe-IT  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 08:30 PM

yes it comes back using the code that i posted above. no offense with your suggestion(the one who says to use datagrid) but i want it to do this way. is there a changes need to be done in the sql query?

This post has been edited by mpe-IT: 11 January 2012 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 08:37 PM

I would loop through the items in ipdr - then set the values of the TextBoxes accordingly.
Interesting reading:
http://msdn.microsof...y/5z06z1kb.aspx
Was This Post Helpful? 0
  • +
  • -

#6 mpe-IT  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 08:41 PM

thanks! i'll try your suggestions. im not good in looping. i'l give it a try :)
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 09:02 PM

If it is not necessary to use those textboxes, use modi123_1 suggestion of using datagrid view to populate your data.
In reality, I see a horrible design what you have used!!! you have 40 records(and in db when we say record we mean db rows), I dont know how many fields your table have!!!!! How are you going to present these data!!! And what if a new record is added to your table? are you going to create another textbox?

Realy think of changing your design and use datagrid view, it is there to help table presentation for you instead of having textboxes equal to number of records you have?

A side question: Are you going to put every row in one text box?
Was This Post Helpful? 0
  • +
  • -

#8 mpe-IT  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 09:04 PM

1 db 1 table 1 column with 40 records. it is only one column.
Was This Post Helpful? 0
  • +
  • -

#9 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 09:07 PM

So 40 rows down? That still won't look very good. A DataGridView is advisable.
EDIT: fixed italics tags. They don't end with [/il]!

This post has been edited by jimmyBo: 11 January 2012 - 09:09 PM

Was This Post Helpful? 0
  • +
  • -

#10 mpe-IT  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 152
  • Joined: 27-November 09

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 09:09 PM

alright, can you give me a link? tutorial on how to retrieve data using datagrid?
Was This Post Helpful? 0
  • +
  • -

#11 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 09:11 PM

Just do a search on it. We can't write the code for you.
http://www.google.co...gridview+vb.net
Was This Post Helpful? -3
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 09:16 PM

I have a snippet of code on that:
http://www.dreaminco...snippet5761.htm
Was This Post Helpful? 2
  • +
  • -

#13 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Retrieve records SQL w/ VB.net

Posted 11 January 2012 - 09:50 PM

Good UI design: unless you really must, don't use tens of TextBoxes - it can get ugly, fast.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10805
  • View blog
  • Posts: 40,278
  • Joined: 27-December 08

Re: Retrieve records SQL w/ VB.net

Posted 12 January 2012 - 01:30 AM

We also have a tutorial on this.

http://www.dreaminco...1&#entry1497754
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1