0

I make a Login page to validate user. I have a sql table like following:

table: tblUsers

userID (int is identity)
username (nvarchr (50))
password (nvarchr (50))

Login.ASPX

    Username<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox><br />
    Password <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox><br />
    <asp:Button ID="cmdLogin" runat="server" Text="Login" 
        onclick="cmdLogin_Click" /><br />
    <asp:Label ID="lblError" runat="server" EnableViewState="False"></asp:Label><br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConStr %>" 
        SelectCommand="SELECT * FROM [tblUser] WHERE (([username] = @username) AND ([password] = @password))">
        <SelectParameters>
            <asp:ControlParameter ControlID="txtPassword" Name="username" 
                PropertyName="Text" Type="String" />
            <asp:ControlParameter ControlID="txtPassword" Name="password" 
                PropertyName="Text" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

Validate users is in SelectCommand of SqlDataSource.

SELECT * FROM [tblUser] WHERE (([username] = @username) AND ([password] = @password))

how to validate user by SqlDataSource? I want code for Login button.

2
  • 4
    Do not store passwords in plain text. More importantly, security is hard. Don't reinvent the wheel. You should use the built-in membership system. Commented Sep 18, 2013 at 14:18
  • Another comment: your SQLDataSource is getting so complex that it will probably do you well to start using the code-behind for your database interaction. ADO.NET has classes that will give you more direct control over your interactions, and that you can put breakpoints to allow you to debug your more complex queries. Highly suggested over using SQLDataSource in the markup. Commented Sep 18, 2013 at 15:04

1 Answer 1

1

Try this:

protected void cmdLogin_Click(object sender, EventArgs e)
{
    DataView dView = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    if (dView.Count == 1)
        Response.Redirect("~/Default.aspx");
    else
        lblError.Text="Incorrect username or password!";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.