Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to retrieve data from mysql database and display it into textbox in c#

how to retrieve data from mysql database and display it into textbox in c#

Scheduled Pinned Locked Moved C#
databasecsharpmysqlsysadmindebugging
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    Xonel
    wrote on last edited by
    #1

    Hi, I have this code below that is not working like i want it to and has no errors when i debug. I want it to display a member values in respective textboxes that are in a table called 'addpeople' by a click of a button AFTER i key in the member ID of a member. Text boxes are like; Title member ID First name Last Name Address 1 Address 2 .... ... ... Email.. Here is the code:

    private void btnView_Click(object sender, EventArgs e)
    {

                string input = cmbMemberId.Text;
                string conn = "server=localhost;user=root;password='';database=m\_chama;";
    
                MySqlConnection myconn = new MySqlConnection(conn);
                
                string sql = "SELECT \* FROM addmember WHERE Member\_ID = '" + input + "';";
    
                
                MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
                DataTable dt = new DataTable();
                da.Fill(dt);
    
                if (dt.Rows.Count == 0)
                {
                    
                    MessageBox.Show("No data found.", "Not Exists");
    
                }
                else
                {
                    foreach (DataRow dr in dt.Rows)
                {
                    if (dr\[0\] + "" == input)
                    {
                        txtFirstName.Text = dr\[0\] + "";
                        break;
                    }
                }
                  
               }
    
            }
    
    A OriginalGriffO P 3 Replies Last reply
    0
    • X Xonel

      Hi, I have this code below that is not working like i want it to and has no errors when i debug. I want it to display a member values in respective textboxes that are in a table called 'addpeople' by a click of a button AFTER i key in the member ID of a member. Text boxes are like; Title member ID First name Last Name Address 1 Address 2 .... ... ... Email.. Here is the code:

      private void btnView_Click(object sender, EventArgs e)
      {

                  string input = cmbMemberId.Text;
                  string conn = "server=localhost;user=root;password='';database=m\_chama;";
      
                  MySqlConnection myconn = new MySqlConnection(conn);
                  
                  string sql = "SELECT \* FROM addmember WHERE Member\_ID = '" + input + "';";
      
                  
                  MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
                  DataTable dt = new DataTable();
                  da.Fill(dt);
      
                  if (dt.Rows.Count == 0)
                  {
                      
                      MessageBox.Show("No data found.", "Not Exists");
      
                  }
                  else
                  {
                      foreach (DataRow dr in dt.Rows)
                  {
                      if (dr\[0\] + "" == input)
                      {
                          txtFirstName.Text = dr\[0\] + "";
                          break;
                      }
                  }
                    
                 }
      
              }
      
      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Try debugging and stepping through your code and figure out if your query is returning correct data. If you are able to go into the else part of your code, check if there are any valid values inside dr[0]. After that you need to populate your textboxes, so you will need code like

      foreach (DataRow dr in dt.Rows)
      {
      if (dr[0] + "" == input)
      {
      txtFirstName.Text = dr[0] + "";
      break;
      }
      txtLastName = dr[1];
      txtTitle = dr[2];
      //and so on

                  }
      

      where the indexes need to match the appropriate values.

      1 Reply Last reply
      0
      • X Xonel

        Hi, I have this code below that is not working like i want it to and has no errors when i debug. I want it to display a member values in respective textboxes that are in a table called 'addpeople' by a click of a button AFTER i key in the member ID of a member. Text boxes are like; Title member ID First name Last Name Address 1 Address 2 .... ... ... Email.. Here is the code:

        private void btnView_Click(object sender, EventArgs e)
        {

                    string input = cmbMemberId.Text;
                    string conn = "server=localhost;user=root;password='';database=m\_chama;";
        
                    MySqlConnection myconn = new MySqlConnection(conn);
                    
                    string sql = "SELECT \* FROM addmember WHERE Member\_ID = '" + input + "';";
        
                    
                    MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
        
                    if (dt.Rows.Count == 0)
                    {
                        
                        MessageBox.Show("No data found.", "Not Exists");
        
                    }
                    else
                    {
                        foreach (DataRow dr in dt.Rows)
                    {
                        if (dr\[0\] + "" == input)
                        {
                            txtFirstName.Text = dr\[0\] + "";
                            break;
                        }
                    }
                      
                   }
        
                }
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        There are two important things to do here: 1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:

                    string sql = "SELECT \* FROM addmember WHERE Member\_ID = @ID;";
                    MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
                    da.SelectCommand.Parameters.AddWithValue("@ID", input);
        
        1. Don't use "*" as the column list in an sql command - particularly if you are going to use numeric indexes. Not only do they make your code liable to fail if the external database is changed, but the order in which SQL returns columns is not defined - when you specify "*" as the columns list SQL is at liberty to return them in any order it desires. Normally it returns them in definition order - but it doesn't have to, and there is no guarantee that future versions will do so. Name your columns in the SELECT (preferred, as it reduces the data returned to just the columns you want), and / or use names for the DataRow index. Implementing these may solve your problem, but you also should not add an empty sting onto the dataRow - it looks silly and is unnecessary. cast it to a string instead:

        foreach (DataRow dr in dt.Rows)
        {
        string iD = (string) dr["iD"];
        if ((string) dr["iD"] == input)
        {
        txtFirstName.Text = (string) dr["FirstName"];
        break;
        }
        }

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        X 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          There are two important things to do here: 1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:

                      string sql = "SELECT \* FROM addmember WHERE Member\_ID = @ID;";
                      MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
                      da.SelectCommand.Parameters.AddWithValue("@ID", input);
          
          1. Don't use "*" as the column list in an sql command - particularly if you are going to use numeric indexes. Not only do they make your code liable to fail if the external database is changed, but the order in which SQL returns columns is not defined - when you specify "*" as the columns list SQL is at liberty to return them in any order it desires. Normally it returns them in definition order - but it doesn't have to, and there is no guarantee that future versions will do so. Name your columns in the SELECT (preferred, as it reduces the data returned to just the columns you want), and / or use names for the DataRow index. Implementing these may solve your problem, but you also should not add an empty sting onto the dataRow - it looks silly and is unnecessary. cast it to a string instead:

          foreach (DataRow dr in dt.Rows)
          {
          string iD = (string) dr["iD"];
          if ((string) dr["iD"] == input)
          {
          txtFirstName.Text = (string) dr["FirstName"];
          break;
          }
          }

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          X Offline
          X Offline
          Xonel
          wrote on last edited by
          #4

          Thank you guyz, You gave me ideas and it worked. :) This is the code that worked...

          private void btnView_Click(object sender, EventArgs e)
          {
          string input = cmbMemberId.Text;
          string conn = "server=[servername];user=[username];password=[userpassword];database=[databasename];";

                  MySqlConnection myconn = new MySqlConnection(conn);
                  string sql = "SELECT Member\_ID, First\_Name, ...., ......, ....., ....., ...., EMAIL FROM addmember WHERE Member\_ID = @ID;";
          
                  MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
                  da.SelectCommand.Parameters.AddWithValue("@ID", input);
                  DataTable dt = new DataTable();
                  da.Fill(dt);
          
                  if (dt.Rows.Count == 0)
                  {
                      MessageBox.Show("No data found.", "Not Exists");
          
                  }
                  else
                  {
                      foreach (DataRow dr in dt.Rows)
                    
                          {
                              if (dr\[0\] + "" == input)
                              {
                                  cmbMemberId.Text = dr\[0\] + "";
                                  txtFirstName.Text = dr\[1\].ToString();
                                  	.....
          			.....
          			.....
          			.....
          			....
          			....
                                  txtEmail.Text = dr\[8\].ToString();
          
                                  break;
                              }
                          
                          }
          
                      }
              }
          

          Thank you soo much.

          OriginalGriffO 1 Reply Last reply
          0
          • X Xonel

            Thank you guyz, You gave me ideas and it worked. :) This is the code that worked...

            private void btnView_Click(object sender, EventArgs e)
            {
            string input = cmbMemberId.Text;
            string conn = "server=[servername];user=[username];password=[userpassword];database=[databasename];";

                    MySqlConnection myconn = new MySqlConnection(conn);
                    string sql = "SELECT Member\_ID, First\_Name, ...., ......, ....., ....., ...., EMAIL FROM addmember WHERE Member\_ID = @ID;";
            
                    MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
                    da.SelectCommand.Parameters.AddWithValue("@ID", input);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
            
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("No data found.", "Not Exists");
            
                    }
                    else
                    {
                        foreach (DataRow dr in dt.Rows)
                      
                            {
                                if (dr\[0\] + "" == input)
                                {
                                    cmbMemberId.Text = dr\[0\] + "";
                                    txtFirstName.Text = dr\[1\].ToString();
                                    	.....
            			.....
            			.....
            			.....
            			....
            			....
                                    txtEmail.Text = dr\[8\].ToString();
            
                                    break;
                                }
                            
                            }
            
                        }
                }
            

            Thank you soo much.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            You're welcome!

            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • X Xonel

              Hi, I have this code below that is not working like i want it to and has no errors when i debug. I want it to display a member values in respective textboxes that are in a table called 'addpeople' by a click of a button AFTER i key in the member ID of a member. Text boxes are like; Title member ID First name Last Name Address 1 Address 2 .... ... ... Email.. Here is the code:

              private void btnView_Click(object sender, EventArgs e)
              {

                          string input = cmbMemberId.Text;
                          string conn = "server=localhost;user=root;password='';database=m\_chama;";
              
                          MySqlConnection myconn = new MySqlConnection(conn);
                          
                          string sql = "SELECT \* FROM addmember WHERE Member\_ID = '" + input + "';";
              
                          
                          MySqlDataAdapter da = new MySqlDataAdapter(sql, myconn);
                          DataTable dt = new DataTable();
                          da.Fill(dt);
              
                          if (dt.Rows.Count == 0)
                          {
                              
                              MessageBox.Show("No data found.", "Not Exists");
              
                          }
                          else
                          {
                              foreach (DataRow dr in dt.Rows)
                          {
                              if (dr\[0\] + "" == input)
                              {
                                  txtFirstName.Text = dr\[0\] + "";
                                  break;
                              }
                          }
                            
                         }
              
                      }
              
              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              What the frack are you doing dr[0] + "" for? :confused: It's pointless. I suspect you just want to cast: if ( **(string)** dr[0] == input) and txtFirstName.Text = **(string)** dr[0] ;

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups