Results 1 to 11 of 11

Thread: how to set a menuItem text properties to a value retrieved from a DB

  1. #1

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    how to set a menuItem text properties to a value retrieved from a DB

    Hi! I am designing an application that will be in french and english. So I will design two table with all the strings I need, on table in french and another in english. Like tbl_french_string and tbl_english_string, with field like STRING_MAIN_MENU_FILE = &File (&Fichier in french). At the application startup, I want to check the language selected and then loaded the right string. Like at startup, if English is selected, then it will loaded the tbl_english_string table and replace all the text with the right string. So there would be som,ething like that, mainMenu1.text = STRING_MAIN_MENU_FILE from the table loaded previously.

    The problem I have is, that I know how to bind data to textboxes, but I dont know how to refer to dataset in the code ..., I dont know either how to create a dataset in the code, because I dont want to have two dataset with the two table, since only just one will be used, depending on what language has been selected. So I'd like to create the dataset at runtime to use the less memory. can anyone help me with this one!??

    I hope I made myself clear, I'll check around, so posted any question and I will answer you if I'm not enogh clear. Thanks in advance!

    Jean Christophe Avard
    Last edited by jcavard; Jul 26th, 2005 at 09:15 AM.

  2. #2
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: how to set a menuItem text properties to a value retrieved from a DB

    Here is an example of creating a dataset in code and filling it with sql.

    VB Code:
    1. Dim dsMnuCaptions As New DataSet
    2.         Dim dtCaptions As New DataTable("Captions")
    3.         Dim sqlDA As New SqlClient.SqlDataAdapter
    4.         Dim sqlCmd As New SqlClient.SqlCommand
    5.  
    6.         sqlCmd.Connection = sqlConn 'here place whatever connection you are using
    7.         sqlDA.SelectCommand = sqlCmd
    8.         dsMnuCaptions.Tables.Add(dtCaptions)
    9.  
    10.         'Put whatever select command you need
    11.         sqlCmd.CommandText = "SELECT STRING_MAIN_MENU_FILE FROM CaptionsTable WHERE Language=1"
    12.         sqlDA.Fill(dsMnuCaptions.Tables("Captions"))
    13.  
    14.         'Use the dataset with whatever columns you selected in the sql query
    15.         MnuItm.Text = dsMnuCaptions.Tables("Captions").Rows(0).Item("SELECT STRING_MAIN_MENU_FILE")

    Hope that points you in the right direction.

  3. #3

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: how to set a menuItem text properties to a value retrieved from a DB

    Thanks a lot dude! but what would be the connection line... I'm using an Access database, oleDbConnection, I'm totally lost with that... it says the connection string asnt been initialized, but when I write: oleCmd.Connection ="Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\InformIT\New\BindControls\DesignTime\Books.mdb"

    it says It cant be a string...

    this is my code now, does it make sense at all?

    VB Code:
    1. Private Sub frmMDI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim dsMnuCaptions As New System.Data.DataSet
    3.         Dim dtCaptions As New System.data.DataTable("tbl_french_string")
    4.         Dim oleDA As New System.data.OleDb.OleDbDataAdapter
    5.         Dim oleCmd As New System.Data.OleDb.OleDbCommand
    6.         Dim oleConn As New System.Data.OleDb.OleDbConnection
    7.  
    8.         'here place whatever connection you are using
    9.         oleCmd.Connection = oleConn
    10.         oleDA.SelectCommand = oleCmd
    11.         dsMnuCaptions.Tables.Add(dtCaptions)
    12.  
    13.         'Put whatever select command you need
    14.         oleCmd.CommandText = "SELECT STRING_MAIN_MENU_FILE FROM tbl_french_string"
    15.         oleDA.Fill(dsMnuCaptions.Tables("tbl_french_string"))
    16.  
    17.         'Use the dataset with whatever columns you selected in the sql query
    18.         'Dim menuitem As New System.Windows.Forms.MenuItem
    19.  
    20.         Me.MenuItem1.Text = dsMnuCaptions.Tables("tbl_french_string").Rows(0).Item("SELECT STRING_MAIN_MENU_FILE")
    21.  
    22.     End Sub


    I gues the "Captions" thing would be the table I query?
    Last edited by jcavard; Jul 26th, 2005 at 11:17 AM.

  4. #4
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: how to set a menuItem text properties to a value retrieved from a DB

    "Captions" can be any word you want. Its just a notation to call the table by name.

    for the connection try this.

    VB Code:
    1. oleConn.ConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;" & _
    2. "Data Source=C:\InformIT\New\BindControls\DesignTime\Books.mdb"
    3.  
    4. oleDA.Connection = oleConn

    If you are using a data adapter I don't think you have to associate the connection with the command, just associate with the adapter that is using the command.

  5. #5
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: how to set a menuItem text properties to a value retrieved from a DB

    Also, if you are still having problems with the connection string, try this -

    In Visual Studio, right-click in Server Explorer and click on 'Add Connection'. Use the form that pops up to create the connection. Then once it is created & working, you can click on the connection and look at the properties window. It will have a property called 'ConnectString' that you should be able to copy to use in your code.

    Or once the connection is created, you can use the oleDBConnection from the toolbox and just drop a connection on your form and set it's connection property to the connection that you created.

  6. #6

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: how to set a menuItem text properties to a value retrieved from a DB

    Thanks a lot dude, really appreciated! I still can't figure this out, I guess once I get that striaght, I will be able to go on my own, but for now, I'm stuck again...

    Wouldn't it be supposed to work... but I get an "An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll" on MSDN they say it's cause by a msising data source, but the connection string I used it the one I created with the wizard... anyone cant point this one?

    VB Code:
    1. Private Sub frmMDI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim dsMnuCaptions As New System.Data.DataSet
    3.         Dim dtCaptions As New System.data.DataTable("tbl_french_string")
    4.  
    5.         Dim oleDA As New System.data.OleDb.OleDbDataAdapter
    6.         Dim oleCmd As New System.Data.OleDb.OleDbCommand
    7.         Dim oleConn As New System.Data.OleDb.OleDbConnection
    8.  
    9.  
    10.         'here place whatever connection you are using
    11.         oleConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jeanca.DSD-LAN\Desktop\Signs PRO\Signs PRO\signspro.mdb;"
    12.         oleCmd.Connection = oleConn
    13.  
    14.  
    15.         oleDA.SelectCommand = oleCmd
    16.         dsMnuCaptions.Tables.Add(dtCaptions)
    17.  
    18.         'Put whatever select command you need
    19.         oleCmd.CommandText = "SELECT STRING_MAIN_MENU_FILE FROM tbl_french_string"
    20.         oleDA.Fill(dsMnuCaptions.Tables("tbl_french_string"))
    21.  
    22.         'Use the dataset with whatever columns you selected in the sql query
    23.         'Dim menuitem As New System.Windows.Forms.MenuItem
    24.  
    25.         Me.MenuItem1.Text = dsMnuCaptions.Tables("tbl_french_string").Rows(0).Item("SELECT STRING_MAIN_MENU_FILE")
    26.  
    27.     End Sub

  7. #7
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: how to set a menuItem text properties to a value retrieved from a DB

    Try changing

    oleCmd.Connection = oleConn

    to

    oleDA.Connection = oleConn

  8. #8

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: how to set a menuItem text properties to a value retrieved from a DB

    I get an error, that's why I had changed it...


    'Connection' is not a member of 'System.Data.OleDb.OleDbDataAdapter'.

  9. #9
    Lively Member
    Join Date
    Jun 2004
    Posts
    99

    Re: how to set a menuItem text properties to a value retrieved from a DB

    How about

    oleDA.SelectCommand.Connection = oleConn

    Sorry bout the wrong code there, I usually work in SQL Server.

  10. #10

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: how to set a menuItem text properties to a value retrieved from a DB

    You don't have to be sorry dude, you've helped me more than I could ask for!

    still I have an "object is not set to an instance of an object" on that line:
    oleDA.SelectCommand.Connection = oleConn

    I'm totally desperate, I remember back in time, VB6 was soooooooo easy, now I just can't find anything in the VStudio help, neither on MSDN website... I look like a newbie, but I still have a degree as a computer analisys... earned it with vb6 though, I'm just way behind what .NET offers... damn, but thank you dude! and all of you!

    i'd like to know, if at least, I've got the right order??

    VB Code:
    1. Dim dsMnuCaptions As New System.Data.DataSet
    2.  
    3.         Dim dtCaptions As New System.data.DataTable("french")
    4.  
    5.         Dim oleDA As New System.data.OleDb.OleDbDataAdapter
    6.         Dim oleCmd As New System.Data.OleDb.OleDbCommand
    7.         Dim oleConn As New System.Data.OleDb.OleDbConnection
    8.  
    9.  
    10.         'here place whatever connection you are using
    11.         oleConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jeanca.DSD-LAN\Desktop\Signs PRO\Signs PRO\signspro.mdb;"
    12.  
    13.  
    14.         oleDA.SelectCommand.Connection = oleConn
    15.         dsMnuCaptions.Tables.Add(dtCaptions)
    16.  
    17.         'Put whatever select command you need
    18.         oleCmd.CommandText = "SELECT STRING_MAIN_MENU_FILE FROM tbl_french_string"
    19.         oleDA.Fill(dsMnuCaptions.Tables("french"))
    20.  
    21.         'Use the dataset with whatever columns you selected in the sql query
    22.         'Dim menuitem As New System.Windows.Forms.MenuItem
    23.  
    24.         Me.MenuItem1.Text = dsMnuCaptions.Tables("french").Rows(0).Item("SELECT STRING_MAIN_MENU_FILE")

  11. #11

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    Re: how to set a menuItem text properties to a value retrieved from a DB

    Finally I got something working fine... except for one reference...

    VB Code:
    1. Imports System.Data
    2. Imports System.Data.OleDb
    3.  
    4. Module _mod
    5.  
    6.  
    7.  
    8.     Function loadLanguageString(ByVal lang)
    9.         Dim parent As frmMDI
    10.  
    11.         ' create a connection string
    12.         Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\jeanca.DSD-LAN\Desktop\Signs PRO\Signs PRO\signspro.mdb"
    13.         Dim myConnection As OleDbConnection = New OleDbConnection
    14.         myConnection.ConnectionString = connString
    15.  
    16.         Dim tblString As String = "tbl_french_string"
    17.  
    18.         ' create a data adapter
    19.         Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from " & tblString, myConnection)
    20.  
    21.         ' create a new dataset
    22.         Dim ds As DataSet = New DataSet
    23.  
    24.         ' fill dataset
    25.         da.Fill(ds, "string")
    26.  
    27.  
    28.         ' write dataset contents to an xml file by calling WriteXml method
    29.         ' Attach DataSet to DataGrid
    30.  
    31.  
    32.  
    33.         parent.MenuItem1.Text = ds.Tables("string").Rows(0).Item(2)
    34.     End Function
    35.  
    36.  
    37. End Module

    How can I refer to a form from the module???


    I get this error
    An unhandled exception of type 'System.NullReferenceException' occurred in Signs PRO.exe

    Additional information: Object reference not set to an instance of an object.


    with this line
    parent.MenuItem1.Text = ds.Tables("string").Rows(0).Item(2)

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