从数据库中检索数据并在文本框中显示

时间:2014-09-29 05:52:53

标签: c# mysql asp.net

我目前正在开发一个Web应用程序,我必须从数据库中获取数据并在文本框中显示它。我有这些领域:

Name,Comp_Name,User_Name,Email_Id,Password,User_Image,Category,Status

并且在它上面我有一个文本框,用户可以输入id来查看数据。例如,如果我在文本框中写1。然后文本框将显示存储在数据库中的id=1记录的数据。

ASPX代码:

  <head runat="server">
  <title>Untitled Page</title>
   <link href="User.css" type="text/css" rel="Stylesheet" />
   <script type="text/javascript" src="imgpreview.js"></script>
  <script type="text/javascript">
 function showimagepreview(input) {
 if (input.files && input.files[0]) {
  var filerdr = new FileReader();
   filerdr.onload = function(e) {
 $('#imgprvw').attr('src', e.target.result);
    }
 filerdr.readAsDataURL(input.files[0]);
}
   }
     </script>
 <style type="text/css">
    #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;
    }
 </style>
 <script>
  window.onload=function()
{
   document.getElementById("button").style.display='none';

  }
    function showButton(){
    document.getElementById("button").style.display='block';
 }


    </script>

    </head>
     <body>
  <form id="form1" runat="server">
 <div id="Full">
 <br />
 <br />
 <br />
 <div id="Main">
 <div id="MainHead">
    <asp:Panel ID="Panel1" runat="server" Height="66px">
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="ID" runat="server" Text="ID"></asp:Label>
                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&        nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
        <input type="text" id="userText" value="Change the text"  runat="server"  />                  <asp:Button 
            ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </asp:Panel>
  </div>
  <br />
  <table style="height: 871px; width: 504px;">
  <tr>
  <td class="style3">

 &nbsp;&nbsp;&nbsp;
    <asp:Label ID="Name" runat="server" Font-Bold="True" ForeColor="Black" 
        Text="Name  :"></asp:Label>

  </td>
  <td class="style2">

    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
   <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
 ControlToValidate="txtName"
 Text="field required!"
  runat="server" />
 </td>
</tr><tr>
<td class="style3">

</td>
<td class="style2">

    &nbsp;</td>
</tr>
      <tr>
   <td class="style3">

    &nbsp;&nbsp;
    <asp:Label ID="CompName" runat="server" Font-Bold="True" ForeColor="Black" 
        Text="Comp Name  :"></asp:Label>

   </td>
  <td class="style2">

    <asp:TextBox ID="txtcomp" runat="server"></asp:TextBox>

   </td>
  </tr>
  <tr>
  <td class="style3">

   </td>
  <td class="style2">

    &nbsp;</td>
    </tr>
    <tr>
   <td class="style3">

    &nbsp;&nbsp;
    <asp:Label ID="UserName" runat="server" Font-Bold="True" ForeColor="Black" 
        Text="User Name  :"></asp:Label>

   </td>
  <td class="style2">

    <asp:TextBox ID="txtuser" runat="server"></asp:TextBox>
   <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
  ControlToValidate="txtUser"
  Text="field required!"
  runat="server" />
  </td>
  </tr>
  <tr>
  <td class="style3">

    &nbsp;</td>
 <td class="style2">

    &nbsp;</td>
  </tr>
  <tr>
    <td class="style3">

    &nbsp;&nbsp;&nbsp;<asp:Label ID="Password" runat="server" Font-Bold="True"      ForeColor="Black" 
        Text="Email-ID  :"></asp:Label>

   </td>
   <td class="style2">

    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
  <asp:RequiredFieldValidator ID="RequiredFieldValidator3"
  ControlToValidate="txtemail"
 Text="field required!"
  runat="server" />
  </td>
  </tr>
   <tr>
  <td class="style3">

    &nbsp;</td>
  <td class="style2">

    &nbsp;</td>
   </tr>
  <tr>
  <td class="style3">

    &nbsp;&nbsp;
    <asp:Label ID="Label5" runat="server" Font-Bold="True" ForeColor="Black" 
        Text="Password  :"></asp:Label>

  </td>
  <td class="style2">

    <asp:TextBox ID="txtpass" runat="server"></asp:TextBox>

  </td>
  </tr>
  <tr>
  <td class="style3">

    &nbsp;</td>
  <td class="style2">

    &nbsp;</td>
  </tr>
  <tr>
  <td class="style4">

    &nbsp;&nbsp;
    <asp:Label ID="Label6" runat="server" Font-Bold="True" ForeColor="Maroon" 
        Text="User Image  :"></asp:Label>

  </td>
  <td class="style5">

    <input type="file" name="filUpload" id="filUpload"     onchange="showimagepreview(this)"  runat="server"/>

  </td>
                    <td class="style6">
                        <asp:Panel ID="Panel2" runat="server" Height="122px">
                        <img id="imgprvw" alt="uploaded image preview"/>
                        </asp:Panel>
                    </td>
  </tr>
  <tr>
  <td class="style3">

    &nbsp;</td>
  <td class="style2">

    &nbsp;</td>
  </tr>
   <tr>
  <td class="style3">

    &nbsp;&nbsp;
    <asp:Label ID="Label7" runat="server" Font-Bold="True" ForeColor="Black" 
        Text="Category  :"></asp:Label>

  </td>
  <td class="style2">

    <asp:TextBox ID="txtcat" runat="server"></asp:TextBox>

   </td>
   </tr>
    <tr>
  <td class="style3">

    &nbsp;</td>
   <td class="style2">

    &nbsp;</td>
    </tr>
    <tr>
   <td class="style3">

    &nbsp;&nbsp;
    <asp:Label ID="Label8" runat="server" Font-Bold="True" ForeColor="Black" 
        Text="Status  :"></asp:Label>

    </td>
   <td class="style2">

    <asp:DropDownList ID="txtddl" runat="server" Height="28px" Width="151px" 
        AutoPostBack="True">
        <asp:ListItem>Store Manager</asp:ListItem>
        <asp:ListItem>Pro Manager</asp:ListItem>
        <asp:ListItem>Engg</asp:ListItem>
        <asp:ListItem>Admin</asp:ListItem>
      </asp:DropDownList>

    </td>
   </tr>
   <tr>
  <td class="style3">

    &nbsp;</td>
  <td class="style2">

    &nbsp;</td>
   </tr>
   <tr>
   <td class="style3">

    &nbsp;&nbsp;&nbsp;<asp:Label ID="Label9" runat="server" Font-Bold="True"     ForeColor="Black" 
        Text="Category No  :"></asp:Label>

                    </td>
   <td class="style2">

    <asp:TextBox ID="txtcatno" runat="server" Height="21px" Width="151px">            </asp:TextBox>
                    </td>
   </tr>
   <tr>
   <td class="style3">

    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    &nbsp;</td>
  <td class="style2">

    &nbsp;</td>
  </tr>
  <tr>
  <td class="style3">

    &nbsp;</td>
  <td class="style2">

    &nbsp;</td>
  </tr>
  <tr>
  <td class="style1" colspan="2">

                           &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&                   nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n      bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp; 
    <asp:Button id="Submit" runat="server" Text="Submit" onclick="Submit_Click"          />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="button" id="btnupd" value="New Button" runat="server" onclick="return    btnupd_onclick()" />
                    </td>
 </tr>
 </table>
 </div>
 </div>
 </form>
 </body>
 </html>

C#代码:

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();
    }
}

1 个答案:

答案 0 :(得分:0)

使用此代码段:

SqlConnection con1 = new SqlConnection("your connection string");
DataTable dt = new DataTable();
con1.Open();
SqlDataReader myReader = null;  
SqlCommand myCommand = new SqlCommand("your query", con1);

myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
    txtName.Text = (myReader["name"].ToString());
    txtEmail.Text = (myReader["email"].ToString());
    //and whatever you have to retrieve
}
con1.Close();