2014-09-29 168 views
-1

我目前正在開發一個Web應用程序,在該應用程序中,我必須從數據庫中獲取數據並在文本框中顯示它。我有這些字段:從數據庫中檢索數據並將其顯示在文本框中

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

和它的頂部我有1個文本框,其中用戶可以輸入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(); 
    } 
} 
+0

所以 - 你嘗試過什麼?你做了什麼研究? – DatRid 2014-09-29 05:56:23

+0

你好datrid.actually我想插入anhd獲取數據在同一form.i我能夠插入數據,但我已經在google上搜索很多,以獲得如何通過id獲取數據,並顯示在文本框中的示例,但所有的例子都是針對gridview和listview的 – 2014-09-29 05:59:32

+0

似乎你並沒有嘗試過somthing,而這聽起來是一個常見的任務,如果你嘗試在網絡上搜索它,你可以找到許多樣本。 – 2014-09-29 06:00:32

回答

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(); 
+0

。代碼沒有顯示任何錯誤,但是在運行時,當我輸入id並單擊按鈕時,它不提取任何數據,它只是重新加載頁面 – 2014-09-29 06:15:49

+0

發佈您爲此編寫的代碼。 – Mairaj 2014-09-29 06:48:10

+0

@codedestructor讓我看看你寫的代碼 – Shaharyar 2014-09-29 06:56:11

相關問題