简体   繁体   English

ASP.NET和SQL Server

[英]Asp.net and Sql server

verify user name and password in login page using asp.net with sql server. 使用带有SQL Server的asp.net在登录页面中验证用户名和密码。 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 和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 您缺少要执行的代码中要执行的sql命令

SqlDataAdapter da = new SqlDataAdapter(cmd); but no where you assign any sql command to cmd 但没有在您将任何sql命令分配给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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM