locked
getting info from a database RRS feed

  • Question

  • I am trying to get info from a database, this info will be stored in variables, my question, is there any difference with binding the data to a variable that binding the data to a textbox for instance.

    Friday, December 1, 2006 2:51 AM

Answers

  • well it depends on how you want to present your data. There are many ways of doing what you are asking.

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=753872&SiteID=1

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=691305&SiteID=1

    one would be getting the data and filling it into a dataset then bind that to a datagridview as shown above or bind it to a textbox:

    Me.theTextBox.DataBindings.Add("Text", theDataSet.Tables(0).DefaultView, "FieldName", true, DataSourceUpdateMode.OnPropertyChanged)

    you could also use a DataReader to read values returned from the query and do whatever you want with them, such as get all values and store it into an array:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=937694&SiteID=1

    converted to VB.NET:

     

    while theDataReader.Read() = true

       Dim currentRowData(theDataReader.FieldCount) as object

       theDataReader.GetValues(currentRowData)

       'theDataTable.Rows.Add(currentRowData)

    end while

    Friday, December 1, 2006 3:15 AM
  • easily done. Using the links above you could do this....

    Dim theCommand as new OleDbCommand("SELECT [Field] FROM [TableName] WHERE [Field] = value", new OleDbConnection(connectionString))

    theCommand.Connection.Open()

    Dim reader as OleDbDataReader = theCommand.ExecuteReader(CommandBehavior.CloseConnection)

    Dim intUserMod as Integer = -1

    if reader.HasRows then

       reader.Read()

       intUserMod = Convert.ToInt32(reader(0)) 'reads the value in the "buffer" and assigns it to the value

    end if

    theCommand.Connection.Close()

     

    this will execute the query and get the result. Now it will only read 1 row of data but you can keep reading the data until no more data by using a while reader.Read() = true loop

    if data has been found, it will read the first column and convert that to an integer so it can assign that value to the variable - you need to be sure that the column is integer so it can convert it correctly

    Friday, December 1, 2006 8:34 PM
  • that was my mistake, I swear I wrote this in......you normally should get an exception thrown since you did not read...

    if reader.HasRows Then

       reader.Read()

       intSTRMod = Convert.ToInt32(reader(1))

    end if

    Saturday, December 2, 2006 4:41 PM

All replies

  • well it depends on how you want to present your data. There are many ways of doing what you are asking.

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=753872&SiteID=1

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=691305&SiteID=1

    one would be getting the data and filling it into a dataset then bind that to a datagridview as shown above or bind it to a textbox:

    Me.theTextBox.DataBindings.Add("Text", theDataSet.Tables(0).DefaultView, "FieldName", true, DataSourceUpdateMode.OnPropertyChanged)

    you could also use a DataReader to read values returned from the query and do whatever you want with them, such as get all values and store it into an array:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=937694&SiteID=1

    converted to VB.NET:

     

    while theDataReader.Read() = true

       Dim currentRowData(theDataReader.FieldCount) as object

       theDataReader.GetValues(currentRowData)

       'theDataTable.Rows.Add(currentRowData)

    end while

    Friday, December 1, 2006 3:15 AM
  • Basically, this is what I want to do:

    I have one textobx that will have a calculated field.  the textbox's calculations are based on the value that is created by a numericupdown, and info that is stored in the database.  It works like this:

    The database holds a value based on certain input from the user, this information is an integer value.  Then a numeric up down box will have a score value, I need to have the numeric up down's score to be decreased or increased by the amount found in the database.

    I would like to use a variable like intUserMod and have the database information fill in the variable with an integer value contained in the database.  I can find the information in the database, get the query to give me the values that I want, and so on

    I would do the equation liek this:

    dim intMod as integer

    dim intUserMod as integer

    '----------

    code that fills intUserMod with database value

    '----------

    intMod = numericupdown.value + intUserMod

    if intMod > 0 and < 2 Then
    textbox1.text = -5
    if intMod > 1 and < 4 Then
    textbox1.text = -4
    if intMod > 3 and < 6 Then
    textbox1.text = -3

    so on, untill the intMod value is < 51

    How would I accomplish this?  **the subs required to do all of this were removed to give a simplified question, I know that I will need to use the events to get the code, and all of this I can do, I just need to know how to fill intUserMod with the DB value.

    As always, thanks in advanced.

    Friday, December 1, 2006 5:42 PM
  • easily done. Using the links above you could do this....

    Dim theCommand as new OleDbCommand("SELECT [Field] FROM [TableName] WHERE [Field] = value", new OleDbConnection(connectionString))

    theCommand.Connection.Open()

    Dim reader as OleDbDataReader = theCommand.ExecuteReader(CommandBehavior.CloseConnection)

    Dim intUserMod as Integer = -1

    if reader.HasRows then

       reader.Read()

       intUserMod = Convert.ToInt32(reader(0)) 'reads the value in the "buffer" and assigns it to the value

    end if

    theCommand.Connection.Close()

     

    this will execute the query and get the result. Now it will only read 1 row of data but you can keep reading the data until no more data by using a while reader.Read() = true loop

    if data has been found, it will read the first column and convert that to an integer so it can assign that value to the variable - you need to be sure that the column is integer so it can convert it correctly

    Friday, December 1, 2006 8:34 PM
  • I used the following code:

     raceModCON.ConnectionString = strProvider & strDBLocation & strRaceModDB
            strRaceModQuery = "SELECT strMod FROM tbl_racial_mods WHERE ID=" & cboRaceValue()
            raceModCMD = New OleDbCommand(strRaceModQuery, raceModCON)

            raceModCMD.Connection.Open()
            Dim reader As OleDbDataReader = raceModCMD.ExecuteReader(CommandBehavior.CloseConnection)

            If reader.HasRows Then

                intSTRMod = Convert.ToInt32(reader(1)) 'reads the value in the "buffer" and assigns it to the value

            End If
            raceModCMD.Connection.Close()

            MessageBox.Show(intSTRMod.ToString)

    and all I get is the -1 that is assigned to the intSTRMod.

    Is there something that I did incorrectly?
    Saturday, December 2, 2006 4:32 PM
  • that was my mistake, I swear I wrote this in......you normally should get an exception thrown since you did not read...

    if reader.HasRows Then

       reader.Read()

       intSTRMod = Convert.ToInt32(reader(1))

    end if

    Saturday, December 2, 2006 4:41 PM
  • Thank you so much, this is exactly what I was looking for. Once again, I appreciate the help.
    Saturday, December 2, 2006 5:30 PM