connect to a database (question)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Claudia Fong

    connect to a database (question)



    HI,


    I'm trying to connect to a db using ADO.NET in C#, but it generate
    error.. I want to display in a combobox the values of one of the field.
    (department name)

    I'm new in C#, so I don't know what happen to the code below:


    OleDbConnection objConnection = new
    OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0; Data
    Source=D:\\db1. mdb");

    OleDbCommand myCommand = new OleDbCommand("s elect DEPT_NAME from
    DEPT__2004 order by DEPT_NAME", objConnection);

    try
    {
    objConnection.O pen();
    OleDbDataReader objReader = myCommand.Execu teReader();





    // fill ComboBox with department name
    while ( objReader.Read( ) )
    {
    comboBox2.Items .Add(
    objReader[ "DEPT_NAME" ] );
    }

    objConnection.C lose(); // close database connection



    }
    catch (Exception ex)
    {
    MessageBox.Show ("Failed");
    }
    finally
    {
    objConnection.C lose();
    }


    Cheers!

    Claudi


    *** Sent via Developersdex http://www.developersdex.com ***
  • ASP.Net programmer

    #2
    Re: connect to a database (question)

    Claudia Fong <cdolphin88@yah oo.co.uk> wrote in
    news:OB$8VcsWFH A.2128@TK2MSFTN GP14.phx.gbl:
    [color=blue]
    >
    >
    > HI,
    >
    >
    > I'm trying to connect to a db using ADO.NET in C#, but it generate
    > error.. I want to display in a combobox the values of one of the
    > field. (department name)
    >
    > I'm new in C#, so I don't know what happen to the code below:[/color]
    [...]

    It would help if you posted the error message. :)

    Comment

    • Nick Weekes

      #3
      Re: connect to a database (question)

      ....and it would also help if you knew the statement producing the
      exception (by stepping into the code F11)...

      Cheers,

      Nick

      Comment

      • Søren Reinke

        #4
        Re: connect to a database (question)

        And replace:
        MessageBox.Show ("Failed");

        with
        MessageBox.Show (ex.Message);

        Gives you a little idea what might be wrong.

        Not good in the final compilation, but for development i always have it on.


        --
        Søren Reinke
        www.Xray-Mag.com/ - Your free diving magazin on the net.
        Current issue Diving in North America, 99 pages.
        Download it in PDF


        "Claudia Fong" <cdolphin88@yah oo.co.uk> wrote in message
        news:OB$8VcsWFH A.2128@TK2MSFTN GP14.phx.gbl...[color=blue]
        >
        >
        > HI,
        >
        >
        > I'm trying to connect to a db using ADO.NET in C#, but it generate
        > error.. I want to display in a combobox the values of one of the field.
        > (department name)
        >
        > I'm new in C#, so I don't know what happen to the code below:
        >
        >
        > OleDbConnection objConnection = new
        > OleDbConnection ("Provider=Micr osoft.Jet.OLEDB .4.0; Data
        > Source=D:\\db1. mdb");
        >
        > OleDbCommand myCommand = new OleDbCommand("s elect DEPT_NAME from
        > DEPT__2004 order by DEPT_NAME", objConnection);
        >
        > try
        > {
        > objConnection.O pen();
        > OleDbDataReader objReader = myCommand.Execu teReader();
        >
        >
        >
        >
        >
        > // fill ComboBox with department name
        > while ( objReader.Read( ) )
        > {
        > comboBox2.Items .Add(
        > objReader[ "DEPT_NAME" ] );
        > }
        >
        > objConnection.C lose(); // close database connection
        >
        >
        >
        > }
        > catch (Exception ex)
        > {
        > MessageBox.Show ("Failed");
        > }
        > finally
        > {
        > objConnection.C lose();
        > }
        >
        >
        > Cheers!
        >
        > Claudi
        >
        >
        > *** Sent via Developersdex http://www.developersdex.com ***[/color]


        Comment

        • Claudia Fong

          #5
          Re: connect to a database (question)



          Hi,

          Thanks, after using the code that you suggest I found out that I
          "misspell" the tables's name :~)


          MessageBox.Show (ex.Message);


          Thanks again!

          Claudi




          *** Sent via Developersdex http://www.developersdex.com ***

          Comment

          Working...