我无法有选择地将数据从数据库带到DropDownList(城市和城镇事物,System.NullReferenceException错误)

时间:2017-09-16 11:08:29

标签: c# asp.net sql-server drop-down-menu

我面临一个小问题,我将在下面告诉您所有细节。如果你帮助我,我会非常高兴。

我的数据库中有3个表作为" tbl_User"," tbl_City"," tbl_Town"。

我的" tbl_User"表:

  • userid int [PK],
  • 电子邮件nv​​archar(50),
  • 密码nvarchar(50),
  • city int,
  • town int

我的" tbl_City"表:

  • cityno int [PK],
  • cityname nvarchar(50)

我的" tbl_Town"表:

  • townno int,
  • townname nvarchar(50),
  • cityno int

如您所见," tbl_City"和" tbl_Town"表彼此相关。这意味着每个城市都有城镇。

  1. 当用户在网站上注册时,他必须选择城市和城镇。所以我可以将城市和城镇保存为" tbl_User"。
  2. 我想要做的是:当用户进入" profile.aspx"时,我希望有选择地在DropDownLists中看到城市和城镇名称。当用户点击DropDownListCity;我希望所有其他城市同时出现。当用户单击DropDownListTown时;我希望显示与所选城市相关的所有城镇。
  3. 我的代码将选择的城市带入" tbl_User"当我点击DropDownListCity时,我可以看到所有其他城市。这里没有问题。但是我的代码没有选择性地带来城镇。我得到了错误:' System.NullReferenceException'。我认为这是因为该城市是在DropDownList中选择的,但程序没有看到所选择的城市。
  4. 我的代码如下:

    Fonksiyon function = new Fonksiyon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetCity();
            GetTown();
            GetCityAndTownSelectively();
        }
    }
    
    private void GetCityAndTownSelectively()
    {
        if (Session["userid"] != null)
        {
            DataRow dr = function.GetDataRow("SELECT tbl_City.cityno, tbl_City.cityname, tbl_Town.townno, tbl_Town.townname FROM tbl_User LEFT JOIN tbl_City on tbl_City.cityno = tbl_User.city LEFT JOIN tbl_Town on tbl_Town.townno = tbl_User.town WHERE userid=" + Session["userid"].ToString());
            if (dr == null)
            {
                Response.Redirect("default.aspx");
            }
            else
            {
                DropDownListCity.ClearSelection();
                DropDownListCity.Items.FindByValue(dr[0].ToString()).Selected = true;
                DropDownListTown.ClearSelection();
                DropDownListTown.Items.FindByValue(dr[2].ToString()).Selected = true;
            }
        }
        else
        {
            Response.Redirect("default.aspx");
        }
    }
    
    private void GetCity()
    {
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("SELECT * FROM tbl_City", conn);
        try
        {
            conn.Open();
            reader = comm.ExecuteReader();
            DropDownListCity.DataSource = reader;
            DropDownListCity.DataValueField = "cityno";
            DropDownListCity.DataTextField = "cityname";
            DropDownListCity.DataBind();
            reader.Close();
        }
        catch
        {
            string message = "<script>alert('Error!');</script>";
            Response.Write(message);
        }
    }
    
    private void GetTown()
    {
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("SELECT * FROM tbl_Town WHERE cityno='" + DropDownListCity.SelectedValue + "'", conn);
        try
        {
            conn.Open();
            reader = comm.ExecuteReader();
            DropDownListTown.DataSource = reader;
            DropDownListTown.DataValueField = "townno";
            DropDownListTown.DataTextField = "townname";
            DropDownListTown.DataBind();
            reader.Close();
        }
        catch
        {
            string message = "<script>alert('Error!');</script>";
            Response.Write(mesaj);
        }
    }
    
    protected void DropDownListCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetTown();
    }
    

    程序在以下行中给出错误:DrpDwnLstTown.Items.FindByValue(dr[2].ToString()).Selected = true;我想我发现错误的原因:当我改变我的GetTown方法SQL查询时这样:SELECT * FROM tbl_Town我的代码带来了城镇有选择地但当我点击DropDownListTown时,我会看到所有城镇。问题是我只能看到与城市相连的城镇。

1 个答案:

答案 0 :(得分:1)

这是您需要的完整代码。

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetCity();

            if (DropDownListCity.Items != null)
            {                    
                GetTown(Convert.ToInt32(DropDownListCity.SelectedValue.ToString()));
            }
        }
    }

    private void GetCity()
    {
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("SELECT * FROM tbl_City order by cityName", conn);
        try
        {
            conn.Open();
            reader = comm.ExecuteReader();
            DropDownListCity.DataSource = reader;
            DropDownListCity.DataValueField = "cityno";
            DropDownListCity.DataTextField = "cityname";
            DropDownListCity.DataBind();
            reader.Close();
        }
        catch
        {
            string message = "<script>alert('Error!');</script>";
            Response.Write(message);
        }



    }



    private void GetTown(Int32  selectedCityNo)
    {
        if (selectedCityNo == 0)
        {
            DropDownListTown.Visible = false;
        }
        else
        {
            SqlConnection conn;
            SqlCommand comm;
            SqlDataReader reader;
            string connectionString = ConfigurationManager.ConnectionStrings["aytasarimConnectionString"].ConnectionString;
            conn = new SqlConnection(connectionString);
            comm = new SqlCommand("SELECT * FROM tbl_Town WHERE cityno='" + selectedCityNo.ToString() + "' order by townname", conn);
            try
            {
                conn.Open();
                reader = comm.ExecuteReader();
                DropDownListTown.DataSource = reader;
                DropDownListTown.DataValueField = "townno";
                DropDownListTown.DataTextField = "townname";
                DropDownListTown.DataBind();
                reader.Close();
            }
            catch
            {
                string message = "<script>alert('Error!');</script>";
                Response.Write(message);
            }


        }
    }

    protected void DropDownListCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlCity = (DropDownList)sender;
        string selectedID = ddlCity.ID;
        DropDownList ddlSelectedCity = (DropDownList)FindControl(selectedID);
        GetTown(Convert.ToInt32(ddlSelectedCity.SelectedValue.ToString()));
    }