看一半代码就行 上下两部分使一样的 谢谢using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["userName"] = null;
Session["loginType"] = null;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "管理员")
{
SqlConnection con = new SqlConnection("server=(local);database=图书馆管理系统;uid=sa;pwd=gqjgqj;");
con.Open();
SqlCommand mycommand = new SqlCommand("select count(*) from 管理员 where 员工号=@loginnum and 登录密码=@password", con); mycommand.Parameters.Add(new SqlParameter("@loginnum", SqlDbType.Char, 8));
mycommand.Parameters["@loginnum"].Value = number;
mycommand.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 20));
mycommand.Parameters["@password"].Value = password;
int i = (int)mycommand.ExecuteScalar();
mycommand.Connection.Close();
if (i > 0)
{
Session["useName"] = number.Text;
Session["loginType"] = DropDownList1.SelectedValue;
Response.Redirect("index.aspx");
}
else
{
Response.Write("<script>alert('登陆账号或密码错误,请核对!')</script>");
}
}
else
{
SqlConnection con = new SqlConnection("server=(local);database=图书馆管理系统;uid=sa;pwd=gqjgqj;");
con.Open(); SqlCommand mycommand = new SqlCommand("select count(*) from 管理员 where 学号=@loginnum and 登录密码=@password", con); mycommand.Parameters.Add(new SqlParameter("@loginnum", SqlDbType.Char, 8));
mycommand.Parameters["@loginnum"].Value = number.Text;
mycommand.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 20));
mycommand.Parameters["@password"].Value = password;
int i =(int) mycommand.ExecuteScalar();
mycommand.Connection.Close();
if (i > 0)
{
Session["useName"] = number;
Session["loginType"] = DropDownList1.SelectedValue;
Response.Redirect("index.aspx");
}
else
{
Response.Write("<script>alert('登陆账号或密码错误,请核对!')</script>");
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
number.Text = "";
password.Text = "";
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["userName"] = null;
Session["loginType"] = null;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "管理员")
{
SqlConnection con = new SqlConnection("server=(local);database=图书馆管理系统;uid=sa;pwd=gqjgqj;");
con.Open();
SqlCommand mycommand = new SqlCommand("select count(*) from 管理员 where 员工号=@loginnum and 登录密码=@password", con); mycommand.Parameters.Add(new SqlParameter("@loginnum", SqlDbType.Char, 8));
mycommand.Parameters["@loginnum"].Value = number;
mycommand.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 20));
mycommand.Parameters["@password"].Value = password;
int i = (int)mycommand.ExecuteScalar();
mycommand.Connection.Close();
if (i > 0)
{
Session["useName"] = number.Text;
Session["loginType"] = DropDownList1.SelectedValue;
Response.Redirect("index.aspx");
}
else
{
Response.Write("<script>alert('登陆账号或密码错误,请核对!')</script>");
}
}
else
{
SqlConnection con = new SqlConnection("server=(local);database=图书馆管理系统;uid=sa;pwd=gqjgqj;");
con.Open(); SqlCommand mycommand = new SqlCommand("select count(*) from 管理员 where 学号=@loginnum and 登录密码=@password", con); mycommand.Parameters.Add(new SqlParameter("@loginnum", SqlDbType.Char, 8));
mycommand.Parameters["@loginnum"].Value = number.Text;
mycommand.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 20));
mycommand.Parameters["@password"].Value = password;
int i =(int) mycommand.ExecuteScalar();
mycommand.Connection.Close();
if (i > 0)
{
Session["useName"] = number;
Session["loginType"] = DropDownList1.SelectedValue;
Response.Redirect("index.aspx");
}
else
{
Response.Write("<script>alert('登陆账号或密码错误,请核对!')</script>");
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
number.Text = "";
password.Text = "";
}
}
解决方案 »
- 求winform监控window服务动向的demo
- c# DateGridView 中RowEnter事件使用错误?详情请进~
- 【请教】label控件设置为透明时切换窗体或者最大化后恢复时label控件闪烁得比较厉害,请问有什么办法解决。
- 我有段简单的代码,高手帮我看看为啥?所有分了
- FarPoint.Web.Spread方面的几个问题?希望大家帮帮忙!
- winfrom中,如何向datagrid中添加控件及datagrid数据向数据库更新的问题?
- 相对路径问题,以及Xml配置文件随dll发布问题。高手请看。
- 一个degug问题,求救
- (新手上路﹗)如何在一個窗體(Form1)上觸發另外一個窗體(Form2)之上 Button1 的事件?
- 请教各位大虾:vs上的编译问题
- c#richTextBox如何添加图片。
- 字符串显示问题
结果集中第一行的第一列;如果结果集为空,则为空引用。你不能这样做类型转换:
int i = (int)mycommand.ExecuteScalar();你可以这样:
int i = (int)mycommand.ExecuteScalar().ToString()
单步跟踪
检查数据类型转换过程有没有错误
检查SqlParameter赋值
using(SqlConnection con = new SqlConnection(""))
{
}
int result = Convert.ToInt32(cmd.ExecuteScalar());
家tostring和Convert.ToInt32都不可以 提示不变
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 对象必须实现 IConvertible。源错误: 只有在调试模式下进行编译时,生成此未处理异常的源代码才会显示出来。若要启用此功能,请执行以下步骤之一,然后请求 URL: 1. 在产生错误的文件的顶部添加一条“Debug=true”指令。例如: <%@ Page Language="C#" Debug="true" %>或:2. 将以下的节添加到应用程序的配置文件中:<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>请注意,第二个步骤将使给定应用程序中的所有文件在调试模式下进行编译;第一个步骤仅使该特定文件在调试模式下进行编译。重要事项: 以调试模式运行应用程序一定会产生内存/性能系统开销。在部署到生产方案之前,应确保应用程序调试已禁用。 堆栈跟踪:
[InvalidCastException: 对象必须实现 IConvertible。]
System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +2556941
System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +414[InvalidCastException: 将参数值从 TextBox 转换到 String 失败。]
System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +943
System.Data.SqlClient.SqlParameter.GetCoercedValue() +29
System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) +97
System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters) +166
System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc) +253
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1005
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteScalar() +137
login.Button1_Click(Object sender, EventArgs e) +753
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746