using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
namespace SkySharp
{
public partial class WebLogonForm : System.Web.UI.Page
{
string constr = "Data Source=admin-pc;Initial Catalog=skyShark;Integrated Security=true";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constr))
{
DataSet dataSet1 = new DataSet();
string username, password;
int datarows = 0;
try
{
SqlCommand cmd = new SqlCommand("select * from dtUsers" + "where Username=@username and Password=@password", con);
username = txtUserName.Text.Trim();
cmd.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar, 15));
username = txtUserName.Text.Trim();
cmd.Parameters.Add("@password", SqlDbType.NVarChar, 15);
password =txtPassword.Text.Trim();
con.Open();
SqlDataAdapter a = new SqlDataAdapter();
a.SelectCommand.Parameters["@username"].Value = username;
a.SelectCommand = cmd;
datarows = a.Fill(dataSet1, "dtUsers");
if (datarows==0)
lblMessage.Text = "Incorrect user name";
else
{
if (dataSet1.Tables["dtUsers"].Rows[0][1].ToString().Trim() == password)
{
lblMessage.Text = "good";
}
else
lblMessage.Text = "Incorrect password";
}
}
catch (SqlException) { }
finally
{
con.Close();
}
}
}
}
}
------解决方案--------------------------------------------------------
a.SelectCommand.Parameters["@username"].Value = username;
a.SelectCommand = cmd;换成cmd.Parameters["@username"].Value = username;
------解决方案--------------------------------------------------------
判断if (datarows==0)
改成下面的
if(dataSet1.tables.rows.count==0)
lblMessage.Text = "Incorrect user name";