类型为' System.Data.SqlClient.SqlException'的未处理异常发生在ASP.NET应用程序中的System.Data.dll中

时间:2014-12-18 20:49:13

标签: asp.net database

我是asp.net的初学者,我开发了一个使用DB的网站,但在尝试连接到数据库时我总是遇到这个异常:

类型' System.Data.SqlClient.SqlException'未处理的异常发生在System.Data.dll

这是代码:

private void button2_Click(object sender, EventArgs e)
{


    SqlConnection con = new SqlConnection("Server = (local)\\ASP2010;Database = accedint_DB;Integrated Security = true");

    SqlCommand com = new SqlCommand();

    com.CommandType = CommandType.Text;
    com.Connection = con;
    com.CommandText = "select EMPLOYEE_ID from EMPLOYEE_INFO where USER_NAME = @Name and USER_PASS = @Pass";


    com.Parameters.AddWithValue("@Name", textBox1.Text);
    com.Parameters.AddWithValue("@Pass", textBox2.Text);


    if (con.State == ConnectionState.Closed)con.Open();


    string UserID;
    UserID = Convert.ToString(com.ExecuteScalar());

    if (UserID =="")
        MessageBox .Show ("Login fail , Please try again");

     else if (comboBox1.Text.ToLower() == "admin")
        {
            Form2 f2 = new Form2();
            f2.EmpID = Convert.ToInt32(UserID);
            f2.EmpName = textBox1.Text;
            f2.Show();
        }
    else if (comboBox1.Text.ToLower() == "employee") {
        Form10 f10 = new Form10();
        f10.EmpID = Convert.ToInt32(UserID);
        f10.EmpName = textBox1.Text;
        f10.Show();

    }

    }

private void button1_Click(object sender, EventArgs e)
{
    if (comboBox1.Text.ToLower() == "admin")
    {
        Form5 f2 = new Form5();

        f2.EmpName = EmpName;

        f2.Show();
    }
    else if (comboBox1.Text.ToLower() == "employee") {

        Form4 f10 = new Form4();
        f10.EmpName = EmpName;
        f10.Show();

    }
}

1 个答案:

答案 0 :(得分:0)

在以下主题下阅读此link

错误“与服务器成功建立连接,但在登录前握手期间发生错误”在连接到SQL Server 2008上的远程SQL Server数据库时发生

希望它有所帮助。