Get value from SQL DB to textbox

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 14564 Views - Last Post: 17 May 2013 - 01:18 PM Rate Topic: -----

#1 arrinay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Get value from SQL DB to textbox

Posted 12 May 2013 - 08:42 AM

Hello,
I have been working on a program to select a random value from a database and show it in a textbox. At the moment I am stuck at the point where I want to get the value from my database into my program itself.
I wanted to generate a random number by using:
 CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)) 

Then I would selecting the primary key which equals the random number, and take the value which is in the same row as the primary key I selected. It's this value that I'd like to show in the textbox. (I hope you understand, I find it hard to explain..)

What is the best way to get my value from the database to the program itself?

(I am fairly new with VB.net and especially with the database stuff, so forgive me for my potential ignorance.)


Is This A Good Question/Topic? 0
  • +

Replies To: Get value from SQL DB to textbox

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Get value from SQL DB to textbox

Posted 12 May 2013 - 09:28 AM

Assuming that you already know how to use a SELECT statement. You could then use a DataReader to read in the fields from the rows that are returned.

This post has been edited by CharlieMay: 12 May 2013 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,760
  • Joined: 12-June 08

Re: Get value from SQL DB to textbox

Posted 12 May 2013 - 09:41 AM

Quote

What is the best way to get my value from the database to the program itself?

(I am fairly new with VB.net and especially with the database stuff, so forgive me for my potential ignorance.)


You should check out the database tutorials!


OleDb Basics in VB.Net

A Really Simple Database Create a Database using Access & VB.net Express 2008
Was This Post Helpful? 0
  • +
  • -

#4 arrinay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Get value from SQL DB to textbox

Posted 12 May 2013 - 09:45 AM

Thank you, I'll read those tutorials trough, since I have yet to understand SELECT-statements.
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes   User is offline

  • Lovable Curmudgeon
  • member icon

Reputation: 600
  • View blog
  • Posts: 3,071
  • Joined: 19-May 09

Re: Get value from SQL DB to textbox

Posted 12 May 2013 - 12:35 PM

If you don't understand select statements, you'll want to start with basic SQL.
Was This Post Helpful? 0
  • +
  • -

#6 arrinay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 08:29 AM

I think I understand the matter a little bit better now. I am only wondering what connectionstring I should use when I am using a "Service-based Database"..
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,760
  • Joined: 12-June 08

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 08:30 AM

Connection strings are more based on brand/type.

http://connectionstrings.com/
Was This Post Helpful? 0
  • +
  • -

#8 arrinay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 08:49 AM

So am I correct when I say that when I use a "service-based database" (made with vb.net itself, when clicking "Add New Item"), I should use a connectionstringtype of:

"Server=.\SQLExpress;AttachDbFilename=FILEPATH\DBname.mdf;Database=DBName; Trusted_Connection=Yes;"

Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,760
  • Joined: 12-June 08

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 08:51 AM

What happens when you try it?
Was This Post Helpful? 0
  • +
  • -

#10 arrinay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 08:56 AM

Nothing happens.. No error was given but there was no output either.. Could be a problem in the rest of the script tough..
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,760
  • Joined: 12-June 08

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 08:58 AM

Make sure you are wrapping your sql interaction points in try/catches...

There's little I can help with if not seeing the code and a vague explanation.. ://>
Was This Post Helpful? 0
  • +
  • -

#12 arrinay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 09:05 AM

Here's the code:

        Dim connectionString As String = "Server=.\SQLExpress;AttachDbFilename=PATH\Database.mdf;Database=Database; Trusted_Connection=Yes;"
        Dim dbCon As SqlConnection = New SqlConnection(connectionString)
        Dim sqlCom As New SqlCommand

        sqlCom.Connection = dbCon
        sqlCom.CommandType = CommandType.Text
        sqlCom.CommandText = "SELECT Name FROM Table WHERE ID=-1"
     
        Try
            Dim dr As SqlDataReader
            dbCon.Open()
            dr = sqlCom.ExecuteReader
            If dr.HasRows = True Then
                txtOutput.Text = CType(dr("ID"), Integer).ToString()
            End If
            dr.Close()
            dbCon.Close()
        Catch ex As Exception
        End Try


Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14168
  • View blog
  • Posts: 56,760
  • Joined: 12-June 08

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 09:08 AM

I do enjoy doing *something* with exceptions.. you know - shoot an error message out to the console, a message box popup, etc. You may as well.

What happens when you put a break point at line one and step through the code?
Was This Post Helpful? 0
  • +
  • -

#14 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 09:32 AM

Pay close attention when stepping through. Especially in the area of If dr.HasRows because if no rows are returned there's nothing to output.

I'm going to suspect that it's not true and is skipping over the block.

You're also missing a key step in using the reader.

This post has been edited by CharlieMay: 14 May 2013 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#15 arrinay   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 12-May 13

Re: Get value from SQL DB to textbox

Posted 14 May 2013 - 10:50 AM

Aha, found an exception:

Directory lookup for the file "PATH\Database.mdf" failed with the operating system error 5(failed to retrieve text for this error. Reason: 15105).
Cannot attach the file 'PATH\Database.mdf' as database 'Database'.

Guess this is caused by a fault in the connectionstring? (I doublechecked the paths, those are correct..)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2