-1

I am returning a data table of one row, then i set the primary key of the datatable and then looking for that column. I then want to assign the value which is returned from the company column into txtCompany.

Here is what I have tried:

private void comboBox1_SelectedItem(object sender, EventArgs e)
{
    MessageBox.Show("Populating form");
    m_dbConnection.Open();

    DataTable dt = new DataTable();
    SQLiteCommand command = m_dbConnection.CreateCommand();
    command.CommandText = "select * from rdpdirectory where company = @company order by company asc";
    //
    command.Parameters.Add(new SQLiteParameter("@company", comboBox1.SelectedText));
    SQLiteDataAdapter db = new SQLiteDataAdapter(command);
    db.Fill(dt);
    //return dt;

    DataColumn[] keyColumns = new DataColumn[1];
    keyColumns[0] = dt.Columns["company"];
    dt.PrimaryKey = keyColumns;

    DataRow row = dt.Rows.Find("company");
    if (row != null)
        txtCompany.Text = row["company"].ToString();
}   
1
  • Can someone show me a better to do this?? Commented Feb 16, 2014 at 8:42

1 Answer 1

-1

I solved it with the following code:

private void comboBox1_SelectedItem(object sender, EventArgs e)
        {
            MessageBox.Show("Populating form");
            m_dbConnection.Open();

            DataTable dt = new DataTable();
            SQLiteCommand command = m_dbConnection.CreateCommand();
            command.CommandText = "select * from rdpdirectory where company = @company order by company asc";
            //
            command.Parameters.Add(new SQLiteParameter("@company", comboBox1.SelectedText));
            SQLiteDataAdapter db = new SQLiteDataAdapter(command);
            db.Fill(dt);
            //return dt;


            DataColumn[] keyColumns = new DataColumn[1];
            keyColumns[0] = dt.Columns["company"];
            dt.PrimaryKey = keyColumns;

            DataRow row = dt.Rows[0];
            txtCompany.Text = row["company"].ToString();
            txtServer.Text = row["server"].ToString();
            txtUserName.Text = row["username"].ToString();
            txtPassword.Text = row["password"].ToString();
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.