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>
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);
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.