Results 1 to 9 of 9

Thread: Assign Variables from MySQL Database Field

  1. #1

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Assign Variables from MySQL Database Field

    I'm looking for a method to assign dynamic variables by using the column names from the MySQL database.

    Code:
       dbread = dbcomm.ExecuteReader()
                While dbread.Read
    
                      ' Me.controls("Label" & i.ToString).text = leg.GetValue(0).ToString
                      Me.controls(dbread.GetName(i)).text As String = dbread.GetValue(i)
    
                End While
    I think this can be done just need a little more advice. What we want to end up with is the column names from the MySQL database become the variable names and the values in the field get assigned to the variable. I'm told this can be done.
    Last edited by Vladamir; Mar 24th, 2014 at 10:47 AM.

  2. #2
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: Assign Variables from MySQL Database Field

    Hi,
    Maybe something along this line of thinking?
    Code:
    Dim variables As New Dictionary(Of String, String)()
    variables(dbread.GetName(i)) =  dbread.GetValue(i)  ' Set the value of the "variable"
    
    Dim value As String = variables(dbread.GetName(i))  ' Retrieve the value of the variable but with column name not dbread, unless u wanted to again
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Assign Variables from MySQL Database Field

    I don't think that's quite what he's looking for...


    @vladamir - did you try your code? did it work? Did it not work? If the name of your controls matches the name of your columns (which I think is what you're looking to do), it should work, IF the control are actually directly ON THE FORM... if they are on a panel, or other container, like a tab or something, then it won't... but only because you're only accessing the form's control collection... you'll need to know what parent the target control is on, and pull it from that control's control collection.

    An alternative is to create a controls collection of your own using a Dictionary, looping recursively through all controls, adding them to your collection, usting the control name as the key. Then you could use that dictionary to retrieve the control back and use it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Assign Variables from MySQL Database Field

    Thanks again everyone. And no I did not test my code...because another developer wrote it and she said it will not work....gee thanks for that. Plus she likes to work with Form Applications thus she's always using controls, etc... This project is a console application which just talks to either other software, a database or a machine out in the shop. No user input allowed. I have to do so much of this kind of automation I'm thinking of starting a new company called NUIA which stands for "No User Input Allowed".

    There is another developer here who specializes in php and he showed us how he does it. To be sure I explain this carefully he basically does his query on the database and then using the names of the columns he assigns those as variable names...not the values of those variables. The values for those variables are then picked up and assigned to the appropriate variables. So in essence he's using the output of his query to create new variables.

  5. #5
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: Assign Variables from MySQL Database Field

    Hi Vladamir,

    Would that not be like my example? its really the ones why to name a variable outside of code. In PHP most likely he would use an associative array which is basically the same as my answer, the first part is the name(or key in PHP) and the second the value. the PHP one is here: http://www.php.net/manual/en/language.types.array.php example number 3.

    otherwise im clearly not understanding what you mean
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Assign Variables from MySQL Database Field

    Ben - yeah... okay... now that I see vlad's explanation and re-read what you did... yeah, using a dictionary is probably as close as you're going to get to that. It's not quite the same as what you can do in PHP ... but it's as close as you're going to get. I guess it's the difference between a script language where you can get away with that, and more typical languages where they are run through a compiler.

    the only down side is that everything in the dictionary has to be the same type. which is probably largely ok, but it can suck if any of them are numbers which need to be used for calculations... then you may run into issues.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: Assign Variables from MySQL Database Field

    You may run into issues but could always just try parse or isnumeric the value to check. PHP still runs through a compiler in order to be shown its just not pre-compiled, however yes PHP allows a fair bit leway.

    I guess this is probably more closer to a PHP $_SESSION variable than it is an array.
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

  8. #8

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Assign Variables from MySQL Database Field

    Thanks again everyone. I'm thinking like you that it's time for me to sharpen my skills using Dictionaries. And as for numerics, this will all be strings, because we're simply looking to establish names for the variables. The values for those variables are mostly strings. And like I'm finding as we develop this project, whether it's a string or numeric value, we simply do the needed conversion(s) when needed. .ToString(), Convert.ToDouble, etc....

    And we have a full blown web developer working with us this time. He's built some big apps for major retailers and commercial property managers. His skills with .NET are limited but his knowledge with Ajax, Java and php are proving invaluable for the user interfaces.

    And beasonsearch... I live in Miami. And they serve what's known as Cortaditos (Cuban Coffee) here. Thus:
    Code:
    If energyNow < 40 then
    Call cortadito
    energyNow = 10^6
    End If

  9. #9
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: Assign Variables from MySQL Database Field

    Quote Originally Posted by Vladamir View Post
    And beasonsearch... I live in Miami. And they serve what's known as Cortaditos (Cuban Coffee) here. Thus:
    Code:
    If energyNow < 40 then
    Call cortadito
    energyNow = 10^6
    End If
    I'm thinking its time to move me and the wife to America, especially near this Cortadito My wife is addicted to coffee or something ;P
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width