title: Retrieve data from database and show it in textboxes
body: <p>I am currently working on a web application in which I have to fetch data from database and show it in text boxes. I have these fields: </p>
  
  <pre><code>Name,Comp_Name,User_Name,Email_Id,Password,User_Image,Category,Status
  </code></pre>
  
  <p>and on top of it I have 1 text box where user can enter the id to see the data. For example if I write <code>1</code> in the textbox. Then the text boxes will show me the data of the record with <code>id=1</code> which is stored in database. </p>
  
  <p><strong>ASPX Code:</strong></p>
  
  <pre><code>  &lt;head runat="server"&gt;
    &lt;title&gt;Untitled Page&lt;/title&gt;
     &lt;link href="User.css" type="text/css" rel="Stylesheet" /&gt;
     &lt;script type="text/javascript" src="imgpreview.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
   function showimagepreview(input) {
   if (input.files &amp;&amp; input.files[0]) {
    var filerdr = new FileReader();
     filerdr.onload = function(e) {
   $('#imgprvw').attr('src', e.target.result);
      }
   filerdr.readAsDataURL(input.files[0]);
  }
     }
       &lt;/script&gt;
   &lt;style type="text/css"&gt;
      #Header
      {
          height: 65px;
      }
      .style1
      {
          height: 37px;
      }
      .style2
      {
          width: 217px;
          height: 37px;
      }
      .style3
      {
          height: 37px;
          width: 155px;
      }
      .style4
      {
          height: 129px;
          width: 155px;
      }
      .style5
      {
          width: 217px;
          height: 129px;
      }
      .style6
      {
          height: 129px;
      }
   &lt;/style&gt;
   &lt;script&gt;
    window.onload=function()
  {
     document.getElementById("button").style.display='none';
  
    }
      function showButton(){
      document.getElementById("button").style.display='block';
   }
  
  
      &lt;/script&gt;
  
      &lt;/head&gt;
       &lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
   &lt;div id="Full"&gt;
   &lt;br /&gt;
   &lt;br /&gt;
   &lt;br /&gt;
   &lt;div id="Main"&gt;
   &lt;div id="MainHead"&gt;
      &lt;asp:Panel ID="Panel1" runat="server" Height="66px"&gt;
          &lt;br /&gt;
          &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
          &lt;asp:Label ID="ID" runat="server" Text="ID"&gt;&lt;/asp:Label&gt;
                        &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;        nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;
          &lt;input type="text" id="userText" value="Change the text"  runat="server"  /&gt;                  &lt;asp:Button 
              ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /&gt;
      &lt;/asp:Panel&gt;
    &lt;/div&gt;
    &lt;br /&gt;
    &lt;table style="height: 871px; width: 504px;"&gt;
    &lt;tr&gt;
    &lt;td class="style3"&gt;
  
   &amp;nbsp;&amp;nbsp;&amp;nbsp;
      &lt;asp:Label ID="Name" runat="server" Font-Bold="True" ForeColor="Black" 
          Text="Name  :"&gt;&lt;/asp:Label&gt;
  
    &lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &lt;asp:TextBox ID="txtName" runat="server"&gt;&lt;/asp:TextBox&gt;
     &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator1"
   ControlToValidate="txtName"
   Text="field required!"
    runat="server" /&gt;
   &lt;/td&gt;
  &lt;/tr&gt;&lt;tr&gt;
  &lt;td class="style3"&gt;
  
  &lt;/td&gt;
  &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
        &lt;tr&gt;
     &lt;td class="style3"&gt;
  
      &amp;nbsp;&amp;nbsp;
      &lt;asp:Label ID="CompName" runat="server" Font-Bold="True" ForeColor="Black" 
          Text="Comp Name  :"&gt;&lt;/asp:Label&gt;
  
     &lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &lt;asp:TextBox ID="txtcomp" runat="server"&gt;&lt;/asp:TextBox&gt;
  
     &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="style3"&gt;
  
     &lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
     &lt;td class="style3"&gt;
  
      &amp;nbsp;&amp;nbsp;
      &lt;asp:Label ID="UserName" runat="server" Font-Bold="True" ForeColor="Black" 
          Text="User Name  :"&gt;&lt;/asp:Label&gt;
  
     &lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &lt;asp:TextBox ID="txtuser" runat="server"&gt;&lt;/asp:TextBox&gt;
     &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator2"
    ControlToValidate="txtUser"
    Text="field required!"
    runat="server" /&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&lt;/td&gt;
   &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class="style3"&gt;
  
      &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;asp:Label ID="Password" runat="server" Font-Bold="True"      ForeColor="Black" 
          Text="Email-ID  :"&gt;&lt;/asp:Label&gt;
  
     &lt;/td&gt;
     &lt;td class="style2"&gt;
  
      &lt;asp:TextBox ID="txtemail" runat="server"&gt;&lt;/asp:TextBox&gt;
    &lt;asp:RequiredFieldValidator ID="RequiredFieldValidator3"
    ControlToValidate="txtemail"
   Text="field required!"
    runat="server" /&gt;
    &lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
     &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&amp;nbsp;
      &lt;asp:Label ID="Label5" runat="server" Font-Bold="True" ForeColor="Black" 
          Text="Password  :"&gt;&lt;/asp:Label&gt;
  
    &lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &lt;asp:TextBox ID="txtpass" runat="server"&gt;&lt;/asp:TextBox&gt;
  
    &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="style4"&gt;
  
      &amp;nbsp;&amp;nbsp;
      &lt;asp:Label ID="Label6" runat="server" Font-Bold="True" ForeColor="Maroon" 
          Text="User Image  :"&gt;&lt;/asp:Label&gt;
  
    &lt;/td&gt;
    &lt;td class="style5"&gt;
  
      &lt;input type="file" name="filUpload" id="filUpload"     onchange="showimagepreview(this)"  runat="server"/&gt;
  
    &lt;/td&gt;
                      &lt;td class="style6"&gt;
                          &lt;asp:Panel ID="Panel2" runat="server" Height="122px"&gt;
                          &lt;img id="imgprvw" alt="uploaded image preview"/&gt;
                          &lt;/asp:Panel&gt;
                      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
     &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&amp;nbsp;
      &lt;asp:Label ID="Label7" runat="server" Font-Bold="True" ForeColor="Black" 
          Text="Category  :"&gt;&lt;/asp:Label&gt;
  
    &lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &lt;asp:TextBox ID="txtcat" runat="server"&gt;&lt;/asp:TextBox&gt;
  
     &lt;/td&gt;
     &lt;/tr&gt;
      &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&lt;/td&gt;
     &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
     &lt;td class="style3"&gt;
  
      &amp;nbsp;&amp;nbsp;
      &lt;asp:Label ID="Label8" runat="server" Font-Bold="True" ForeColor="Black" 
          Text="Status  :"&gt;&lt;/asp:Label&gt;
  
      &lt;/td&gt;
     &lt;td class="style2"&gt;
  
      &lt;asp:DropDownList ID="txtddl" runat="server" Height="28px" Width="151px" 
          AutoPostBack="True"&gt;
          &lt;asp:ListItem&gt;Store Manager&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;Pro Manager&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;Engg&lt;/asp:ListItem&gt;
          &lt;asp:ListItem&gt;Admin&lt;/asp:ListItem&gt;
        &lt;/asp:DropDownList&gt;
  
      &lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
     &lt;td class="style3"&gt;
  
      &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;asp:Label ID="Label9" runat="server" Font-Bold="True"     ForeColor="Black" 
          Text="Category No  :"&gt;&lt;/asp:Label&gt;
  
                      &lt;/td&gt;
     &lt;td class="style2"&gt;
  
      &lt;asp:TextBox ID="txtcatno" runat="server" Height="21px" Width="151px"&gt;            &lt;/asp:TextBox&gt;
                      &lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
     &lt;td class="style3"&gt;
  
      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    &amp;nbsp;&lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="style3"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;td class="style2"&gt;
  
      &amp;nbsp;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td class="style1" colspan="2"&gt;
  
                             &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;                   nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;n      bsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
      &amp;nbsp; 
      &lt;asp:Button id="Submit" runat="server" Text="Submit" onclick="Submit_Click"          /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
      &lt;input type="button" id="btnupd" value="New Button" runat="server" onclick="return    btnupd_onclick()" /&gt;
                      &lt;/td&gt;
   &lt;/tr&gt;
   &lt;/table&gt;
   &lt;/div&gt;
   &lt;/div&gt;
   &lt;/form&gt;
   &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>
  
  <p><strong>C# Code:</strong></p>
  
  <pre><code>using System;
  using System.Configuration;
  using System.Data;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using MySql.Data.MySqlClient;
  using System.Windows.Forms;
  
  public partial class _Default : System.Web.UI.Page
  {
      MySqlConnection con = new MySqlConnection("DataSource=localhost;database=mrp;userid=**;password=**;port=**");
  
      protected void Page_Load(object sender, EventArgs e)
      {
  
      }
  
      protected void Submit_Click(object sender, EventArgs e)
      {
        try
         {
              //This is my connection string i have assigned the database file address path
              string MyConnection2 =     "datasource=localhost;port=**;username=**;password=**;database=mrp";
              //This is my insert query in which i am taking input from the user through     windows forms
              string Query = "insert into user_tbl(Name,Comp_Name,User_Name,Email_Id,Password,User_Image,Category,Status) values('" +   this.txtName.Text + "','" + this.txtcomp.Text + "','" + this.txtuser.Text + "','" +    this.txtemail.Text + "','" + this.txtpass.Text + "','" + this.filUpload.Value + "','" +     this.txtcat.Text + "','" + this.txtddl.Text + "');";
              //This is  MySqlConnection here i have created the object and pass my      connection string.
               MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
  
              //This is command class which will handle the query and connection object.
              MySqlCommand MyCommand2 = new MySqlCommand(Query, MyConn2);
              MySqlDataReader MyReader2;
              MyConn2.Open();
              MyReader2 = MyCommand2.ExecuteReader();     // Here our query will be executed            and data saved into the database.
              MessageBox.Show("Save Data");
              while (MyReader2.Read())
              {
  
              }
              MyConn2.Close();
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          MySqlConnection con1 = new MySqlConnection("Data      Source=localhost;database=mrp;userid=**;password=**;port=**");
          var str = "select * from user_tbl,con1";
          DataTable dt = new DataTable();
  
          con1.Open();
          MySqlDataReader myReader = null;
          MySqlCommand myCommand = new MySqlCommand("Select * from user_tbl where Id=" + Id1, con1);
          myReader = myCommand.ExecuteReader();
  
          while (myReader.Read())
          {
              txtName.Text = (myReader["Name"].ToString());
              txtemail.Text = (myReader["Email_Id"].ToString());
              txtddl.Text = (myReader["Status"].ToString());
              txtpass.Text = (myReader["Password"].ToString());
              txtcomp.Text = (myReader["Comp_Name"].ToString());
              txtuser.Text = (myReader["User_Name"].ToString());
              filUpload.Value = (myReader["User_Image"].ToString());
  
              //and whatever you have to retrieve
          }
         con1.Close();
      }
  }
  </code></pre>