早上好
我最近在学习C#和.net并尝试制作注册页面。
IF Username ='HelloWorld1'和Password ='密码'
基本上我尝试测试一下,如果用户是管理员或我已在数据库中硬编码的员工,
然后会出现此测试框
然后
<!--this is html code -->
<tbody id ="tbody" runat="server" visible ="false">
<tr>
<td class="auto-style1">IsEmployee</td>
<td class="auto-style1">
<asp:CheckBox ID="CheckBox1" runat="server" />
</td>
<td class="auto-style1">
</td>
</tr>
<tr>
<td>IsAdmin</td>
<td>
<asp:CheckBox ID="CheckBox2" runat="server" />
</td>
<td> </td>
</tr>
</tbody>
显示
我的代码是
protected void CustomValidator3_ServerValidate(object source, ServerValidateEventArgs args)
{
String Artists = System.Configuration.ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(Artists);
SqlCommand objcmd = new SqlCommand("Select * from dbo.UserLoggins where UserName ='HelloWorld1' and Password ='password'", con);
SqlDataReader objReader;
con.Open();
objReader = objcmd.ExecuteReader();
if (objReader.HasRows)
{
args.IsValid = false;
tbody.Visible = true;
args.IsValid = true;
}
con.Close();
}
如何使此代码生效?
非常感谢,祝你有美好的一天。
答案 0 :(得分:1)
嗯,是的,有问题,但是如果你只是想让你的例子工作试试这个。假设您正确配置了自定义验证器控件,您只想返回IsValid是真还是假。
int count = 0;
using (SqlConnection con = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["FleetManagementConnectionString"].ConnectionString))
{
SqlCommand command = new SqlCommand("Select Count(*) from dbo.UserLoggins where UserName =@User and Password =@Password", con);
SqlParameter paramName = new SqlParameter("@User", SqlDbType.VarChar, 255) { Value = "HelloWorld1" };
command.Parameters.Add(paramName);
SqlParameter paramName = new SqlParameter("@Password", SqlDbType.VarChar, 255) { Value = "password" };
command.Parameters.Add(paramName);
con.Open();
count = (int)cmd.ExecuteScalar();
con.Close();
}
args.IsValid = count > 0;
return;