none
C# View certain data from MS Access RRS feed

  • Question

  • Hi All,

    I am trying to view a certain row from Access in my C# program.

    private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                OdbcConnection conn = new OdbcConnection();
                OdbcDataAdapter da = new OdbcDataAdapter();
                conn.ConnectionString = "Dsn=assdb";

                conn.Open();

                var cmd = new OdbcCommand("SELECT * FROM AssetType", conn);
                OdbcDataReader rdr = cmd.ExecuteReader();

                if (textBox.Text == "1")
                {
                    rdr.Read();
                    string AssetType = textBox2.Text;
                    string AssetTypeDesc = textBox3.Text;

                    cmd.CommandType = CommandType.Text;

                    cmd = new OdbcCommand("INSERT INTO AssetType (at_AssetType, at_AssetTypeDescription) VALUES ('" + AssetType + "', '" + AssetTypeDesc + "');", conn);
                    cmd.ExecuteNonQuery();

                    rdr.Close();
                    conn.Close();

                    textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = "";
                }

                else if(textBox.Text == "2")
                {
                    rdr.Read();

                    cmd.CommandType = CommandType.Text;

                    cmd = new OdbcCommand("SELECT * FROM AssetType", conn);
                    cmd.ExecuteNonQuery();
                    rdr = cmd.ExecuteReader();

                    MessageBox.Show("" + rdr);

                    for (int i = 0; i < rdr.FieldCount; i++)
                    {
                        string colName = rdr.GetString(2);
                        MessageBox.Show(colName);

                        //textBox1.Text = "" + rdr["at_ID"].ToString();
                        //textBox2.Text = "" + rdr["at_AssetType"].ToString();
                        //textBox3.Text = "" + rdr["at_AssetTypeDescription"].ToString();
                    }

                    rdr.Close();
                    conn.Close();
                }
            }

    thats my code, I have tried so many different techniuqes, anyone know a simple method for this.

    my table is at_ID, at_AssetType, at_AssetTypeDescription.

    Cheers :)

    Friday, June 7, 2013 11:56 AM

Answers

  • Hi welshsailor,

    You can either use OdbcCommand to execute sql queries or use OdbcDataAdapter to update a DataSet.

    I think you can use this OdbcCommand to implement the features.

    What I think is that rdr may be not really needed in your code, and you are doing the same when textBox.Text == "2". I suggest you to move conn.Close() out of the if .. else .. statement, the conn will also be closed if textBox.Text isn't "1" or "2". You can also try to use a ComboBox instead of textbox, if (textbox.Text == "1") seems a little hard-coded.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 11, 2013 9:17 AM
    Moderator