0

verify user name and password in login page using asp.net with sql server. But problem is that when i enter the correct data ..go to the error page, not go to the welcom page..

  using System;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace WebApplication21

    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {

            }
           protected void Button1_Click(object sender, EventArgs e)
            {

             SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
            con.Open();
         SqlCommand cmd = new SqlCommand("select * from 'user_insert' where username = @username and password = @password,con");
              cmd.Parameters.AddWithValue("@username", TextBox1.Text);
              cmd.Parameters.AddWithValue("@password", TextBox2.Text);
          SqlDataAdapter da = new SqlDataAdapter(cmd);
          DataTable dt = new DataTable();
          da.Fill(dt);

          if (dt.Rows.Count > 0)
          {
              Response.Redirect("Welcom.aspx");
          }
          else
          {
              Response.Redirect("Error.aspx");
          }

    }

And Connection String in web.config

<connectionStrings>
<add name="dbconnection" connectionString="Data Source=Ali-PC;Initial Catalog=LogIn;Integrated Security=True"/>
</connectionStrings>
3
  • What have you set cmd.CommandText to? Where are you defining cmd? Commented May 17, 2015 at 17:44
  • you r not setting the command to execute..sqlcommand cmd = new sqlcommand("select * from 'your table' where username = username and password = @password,con); Commented May 17, 2015 at 17:53
  • 2
    plain-text passwords make sad. It's wrong. Passwords must be stored hashed and salted. Commented May 17, 2015 at 18:39

2 Answers 2

1

Execute the query and you will get the result in your data table use below code instead of yours:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from 'user_insert' where username = @username and password = @password,con");
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
cmd.Parameters.AddWithValue("@password", TextBox2.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
da.Fill(dt);
Sign up to request clarification or add additional context in comments.

Comments

0

You are missing the sql command to execute in your code you are executing

SqlDataAdapter da = new SqlDataAdapter(cmd); but no where you assign any sql command to cmd

try like this and check the result

      SqlCommand cmd = new SqlCommand
    (@"select * from 'your table' where username = @username and password = @password",con);
cmd.Parameters.AddWithValue("@username", TextBox1.Text);
          cmd.Parameters.AddWithValue("@password", TextBox2.Text);

          DataTable dt = new DataTable();
          dt.Load(cmd.ExecuteReader());

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.