登入界面和数据库的链接代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;public partial class _Default : System.Web.UI.Page
{
     string connectionString = WebConfigurationManager.ConnectionStrings["SqlDataSource1"].ConnectionString;    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand comd;
        SqlDataReader dr;        conn.Open();       
        string username = TextBox1.Text.Trim();
        string password = TextBox2.Text.Trim();
        string cmdStr = "select * from MembersInfor where Name='" + username + "' and Password='" + password + "'";
        comd = new SqlCommand(cmdStr, conn);
        dr = comd.ExecuteReader();
        if(dr.Read())
        {
            Response.Redirect("MainPage.aspx");
        }
        conn.Close();    }
}也没说代码哪里错了,该怎么解决呢??Stack Trace: 
[NullReferenceException: Object reference not set to an instance of an object.]
   _Default..ctor() +28
   ASP.default_aspx..ctor() +13
   __ASP.FastObjectFactory_app_web_h3xamzgw.Create_ASP_default_aspx() +20
   System.Web.Compilation.BuildResultCompiledType.CreateInstance() +32
   System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) +119
   System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33
   System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +40
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +160
   System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
 

解决方案 »

  1.   

    仔细看了一下代码完全没问题!!
    用其他方法是否能访问数据库.例如用SqlDataAdapter适配器!
    如果也不能访问话,
    看一下你的vs打过了补丁了吗??
      

  2.   

    加try vatch
    单步跟踪看看
    using(SqlConnection conn = new SqlConnection(connectionString))
    {
    conn.Open(); 
    string cmdStr = "select * from MembersInfor where Name='" + TextBox1.Text.Trim()+ "' and Password='" + TextBox2.Text.Trim()+ "'";
    SqlCommand comd= new SqlCommand(cmdStr, conn);
    SqlDataReader dr= comd.ExecuteReader(CommandBehavior.CloseConnection);
    if(dr.HasRows)
    {
    Response.Redirect("MainPage.aspx");
    }
    conn.Close();
    }
    使用参数SqlParameter