我正在 Visual studio.net 2010 中编写登录页面。(c#) 我创建了一个包含一些方法的类。这包括 :
namespace SchoolsNetwork
{
public class cUsers2
{
SqlConnection SchoolsDBConnectionString;
SqlDataAdapter sda;
SqlCommandBuilder scb;
SqlConnection(ConfigurationManager.ConnectionStrings
"SchoolsDBConnectionString"].ConnectionString);
public DsUsers2 Checkpassword(string UserName, string password,out bool Success)
{
DsUsers2 dsUsers=new DsUsers2();
string SqlStr="select * from tblMembers where Username='"+UserName+"' and Password='"+password+"'";
sda.SelectCommand=new SqlCommand(SqlStr,SchoolsDBConnectionString);
sda.Fill(dsUsers.tblMembers);
Success=dsUsers.tblMembers.Rows.Count>0;
return dsUsers ;
}
然后在我的 form.aspx 页面中,我有用于输入用户名和密码的文本字段以及用于单击登录的按钮。按钮的代码是:
protected void Button1_Click(object sender, EventArgs e)
{
if (txtuser.Text.Trim().Length>0 && txtpass.Value.Length>0 )
{
cUsers2 cUsers=new cUsers2();
DsUsers2 dsUser=new DsUsers2();
bool Success;
if (txtuser.Text.Trim()=="admin")
{
Success=cUsers.CheckAdminPass(txtuser.Text.Trim(),txtpass.Value.Trim());
if (Success)
{
Response.Redirect("WebForm2.aspx");
}
}
dsUser=cUsers.Checkpassword(txtuser.Text.Trim(), txtpass.Value.Trim(),out Success);
if(Success)
{
Session["ID"]=dsUser.tblMembers.Rows[0][dsUser.tblMembers.IDUserColumn].ToString();
System.Web.HttpContext.Current.Response.Redirect("frmProfile.aspx");
}
else lblerror.Text="invalid username & password";
}
}
在 web.config 我有这个作为连接字符串:
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="SchoolsDBConnectionString" connectionString="
Data Source=Sunny-LPTP\SQLEXPRESS;Initial Catalog=SchoolsDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
调试后,此行被标记为错误:
sda.SelectCommand=new SqlCommand(SqlStr,SchoolsDBConnectionString);
它说
你调用的对象是空的。
谁能帮帮我吗?谢谢