Fetch Password from Database based on Login Username in ASP.Net

Last Reply on Jul 10, 2013 05:07 AM By ashusingh

Posted on Jul 10, 2013 04:54 AM

Sir I  want to show the passwor from database into Lable,I have wright code following

 

SqlConnection con = new SqlConnection(@"Data Source=TEGADEV1\SQL2K5EXPR;Initial Catalog=mydatabase;Integrated Security=True;Pooling=False");
SqlCommand cmd = new SqlCommand("select password from reg where username='" + username + "'", con);

cmd.Parameters.AddWithValue("@username", username);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

Lable.Text=......

I have use datatable how to read data from above code what will be the code wright Lable.Text=...

Posted on Jul 10, 2013 05:07 AM

Hi @band.atul

try the following sample

Html

<div>
        <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnShowPassword" runat="server" Text="Show Password" OnClick="ShowPassword" />
        <asp:Label ID="lblPassword" runat="server"></asp:Label>
    </div>

Code

  protected void ShowPassword(object sende, EventArgs e)
        {
            DataTable newDT = this.GetPassword();
            this.lblPassword.Text = "Password:" + newDT.Rows[0]["Password"].ToString();
        }

        private DataTable GetPassword()
        {
            SqlConnection con = new SqlConnection("Data Source=192.168.0.1\\SQL2005;Initial Catalog=Sample;User Id=sa;Password=pass@123");
            SqlCommand cmd = new SqlCommand("SELECT Password FROM Table_Users WHERE UserName=@UserName", con);
            cmd.Parameters.Add("@UserName", this.txtUserName.Text.Trim());
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }

Namespaces

using System;
using System.Data.SqlClient;
using System.Data;