Question Display Data In Text Box Based On ID

Rabastan

New member
Joined
Dec 24, 2014
Messages
1
Programming Experience
Beginner
I have a area in my Project where i need to display a title and content in two text boxes. The Data base tables are:

ID | Title | Content

This is what I have so far, I know I am on the right track, I just cant figure out how to get each box to display based on the ID.
C#:
private void frmMain_Load(object sender, EventArgs e)
{
    try
    {
        string connStr = ConfigurationManager.ConnectionStrings["sdcAssistDB"].ConnectionString;
        OleDbConnection dbConn = new OleDbConnection(connStr);
        dbConn.Open();
        OleDbCommand dbCommand = new OleDbCommand();
        dbCommand.Connection = dbConn;
        string query = "select * from JobAid";
        dbCommand.CommandText = query;
        OleDbDataReader dbReader = dbCommand.ExecuteReader();
        while (dbReader.Read())
        {
            txtTitle1.Text = (dbReader["Title"].ToString());
            txtContent1.Text = (dbReader["Content"].ToString());
            txtTitle2.Text = (dbReader["Title"].ToString());
            txtContent2.Text = (dbReader["Content"].ToString());
        }
        dbConn.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Unable to Connect to Database" + Environment.NewLine + "SDC Assistant will now Close " +ex,
            "Critical Database Error!",
            MessageBoxButtons.OK,
            MessageBoxIcon.Error);
            Application.Exit();
    }
}


Thank you in advance
Rab
 
I think you need to provide a FULL and CLEAR explanation of exactly what you expect to happen. Are you simply asking how to query a database for a specific record by ID or more than that, because the code you have right now doesn't really make sense. It's loading the form, retrieving all the records from a table and then laboriously loading just the last one into two sets of TextBoxes. Don't expect us to work out what you want to do from code that doesn't actually do it.
 

Latest posts

Back
Top Bottom