且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取错误“对象引用未设置为对象的实例”。

更新时间:2023-11-30 22:03:22

试试这样



Try like this

protected void btnLogin_Click(object sender, EventArgs e)
  {

      DataSet ds = new DataSet();
      DataTable dt = new DataTable();
      SqlDataAdapter adp = new SqlDataAdapter();

      using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
      {
          SqlCommand cmd = new SqlCommand("Login_Check_Sp", conn);
          cmd.Parameters.AddWithValue("@username", txtUserName.Text.Trim());
          cmd.Parameters.AddWithValue("@pwd", txtPwd.Text.Trim());

          cmd.CommandType = CommandType.StoredProcedure;

          SqlDataAdapter da = new SqlDataAdapter();
          da.SelectCommand = cmd;
          da.Fill(ds);
          dt = ds.Tables[0];

          if (dt.Rows.Count > 0)
          {
              lblStatus.Text = "Login Successfull";

          }
          else
          {
              lblStatus.Text = "Wrong Username/Password";

          }
      }



  }





如果你有错误,请尝试上面的代码。

调试a和使用断点检查你得到错误。

可能有很多原因因为连接可能没有建立。

这里有一些链接解释了如何在视觉工作室中使用断点。



http://www.tutorialspoint.com/asp.net/asp.net_debugging.htm [ ^ ]



https://www.***.com/watch?v=k1EQndVmlOc [ ^ ]