question

VAer-4038 avatar image
0 Votes"
VAer-4038 asked DanielZhang-MSFT commented

How to display TextBox/ComboBox value from database table?

There is a database table TableUser, and there are four fields: FirstName, LastName, Country, State. Actually, there is another field Username, which does not require user to provide, it is ENvironment.Name

When user submits his/her information, first three fields are required, and State is optional.

Now in another form, user wants to update his/her information (It is guaranteed that the user is in database).

Two parts of question:
How to display the record value back into TextBox/ComboBox?
When user updates his/her information, how to write the code to update information in database? It is same that first three fields are required and State is optional.

Thanks.

     private void ChildForm1_Load(object sender, EventArgs e)
            {
                   
                
                OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString);
                Cn.Open();
                
  string query = "SELECT * from TableUser WHERE Username = '" + ENvironment.Name + "'";
  //It is guaranteed that there is one record
    
  TextboxFirstName.Text = FirstName from returned query; there is a value
  TextboxLastName.Text = LastName from returned query; there is a value
    
  ComboBoxCountry.Text = Country from returned query; there is a value
  ComboBoxState.Text = State from returned query; It may be missing/null
    
                
                Cn.Close();
                
                
            }
windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT commented

Hi VAer-4038,
For question1, you can try the following the code:

 using (OdbcConnection connection = new OdbcConnection())
  {
         connection.ConnectionString = "Your connectionString";
         connection.Open();
         OdbcCommand command = new OdbcCommand("SELECT * from TableUser WHERE Username = '" + ENvironment.Name + "'", connection );
         // Execute the DataReader and access the data.
         OdbcDataReader dr = command.ExecuteReader();
         while (dr.Read())
         {
            TextboxFirstName.Text = dr["FirstName"].ToString();
            TextboxLastName.Text = dr["LastName "].ToString();
         }
 }

For question2, you can use OdbcCommand.ExecuteNonQuery method to execute an SQL statement against the connection.
And here is code example i this document you can refer to.
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is the difference between my code Cn.Close() and OdbcCommand.ExecuteNonQuery ? Question 2 is about updating record, user may change FirstName/LastName/Country/State.

What if I also want to write database table value to combo box (as below)? What if dr["State "] is NULL? A record must have FirstName/LastName/Country, but State is optional.

Thanks.

          while (dr.Read())
          {
             TextboxFirstName.Text = dr["FirstName"].ToString();
             TextboxLastName.Text = dr["LastName "].ToString();
             comboBox1.Text= dr["Country "].ToString();
                comboBox2.Text= dr["State "].ToString();
    
    
          }
0 Votes 0 ·

Hi @VAer-4038 ,
OdbcConnection.Close method is used to close the connection to the data source.
And OdbcCommand.ExecuteNonQuery is used to execute an SQL statement against the Connection.
You can use comboBox.Text property to set its value.
Then you can use the if statement to determine whether dr["State "] is null.

 if (dr["State "] == null)
 {
     comboBox2.Text = "";
 }
 else
 {
     comboBox2.Text = dr["State "].ToString();
 }

Best Regards,
Daniel Zhang



0 Votes 0 ·