看一半代码就行 上下两部分使一样的    谢谢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 = "";
    }
}

解决方案 »

  1.   

    ExecuteScalar()方法返回的是Object
    结果集中第一行的第一列;如果结果集为空,则为空引用。你不能这样做类型转换:
    int i = (int)mycommand.ExecuteScalar();你可以这样:
    int i = (int)mycommand.ExecuteScalar().ToString()
      

  2.   

    之所提示:对象必须实现 IConvertible错误,是以为object没有实现转换成int的方法。
      

  3.   

    哪里提示
    单步跟踪
    检查数据类型转换过程有没有错误
    检查SqlParameter赋值
    using(SqlConnection con = new SqlConnection(""))
    {
    }
    int result = Convert.ToInt32(cmd.ExecuteScalar()); 
      

  4.   

    两种方法都用过了  并不行啊
    家tostring和Convert.ToInt32都不可以  提示不变
      

  5.   

    这是错误提示
    说明: 执行当前 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
     
      

  6.   

    你看看toString后,先不转成int,看看字符串是什么?
      

  7.   

    postbackurl属性设置问题 真晕  整我一下午  还是谢谢大家