我不能选择性地将数据从数据库带到DropDownList和所有其他数据同时

时间:2017-09-15 09:29:53

标签: 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”表彼此相关。

当用户在网站上注册时,他必须选择城市和城镇。所以我可以在“tbl_User”中将城市和城镇保存为号码。 我想要做的是:当用户使用“profile.aspx”时,我希望有选择地在DropDownList中看到城市和城镇,当用户点击DropDownListCity或DropDownListTown时我想要所有其他城市和城镇同时出现。我的代码在“tbl_User”中选择了城市,当我点击DropDownListCity时,我可以看到所有其他城市。但是我的代码没有选择城镇因为“tbl_City”和“tbl_Town”表是相关的(当我按下DropDownListCity时,DropDownListTown必须相应地改变)。我的代码如下:

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
        {
            DrpDwnLstCity.ClearSelection();
            DrpDwnLstCity.Items.FindByValue(dr[0].ToString()).Selected = true;
            DrpDwnLstTown.ClearSelection();
            DrpDwnLstTown.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();
        DrpDwnLstCity.DataSource = reader;
        DrpDwnLstCity.DataValueField = "cityno";
        DrpDwnLstCity.DataTextField = "cityname";
        DrpDwnLstCity.DataBind();
        reader.Close();
    }
    catch
    {
        string mesaj = "<script>alert('Error!');</script>";
        Response.Write(mesaj);
    }
}

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='" + DrpDwnLstCity.SelectedValue + "'", conn);
    try
    {
        conn.Open();
        reader = comm.ExecuteReader();
        DrpDwnLstTown.DataSource = reader;
        DrpDwnLstTown.DataValueField = "townno";
        DrpDwnLstTown.DataTextField = "townname";
        DrpDwnLstTown.DataBind();
        reader.Close();
    }
    catch
    {
        string mesaj = "<script>alert('Error!');</script>";
        Response.Write(mesaj);
    }
}

protected void DrpDwnLstCity_SelectedIndexChanged(object sender, EventArgs e)
{
    GetTown();
}

我收到错误:aytasarim.dll中发生了'System.NullReferenceException'类型的异常,但未在用户代码中处理。

编辑:当我更改我的GetTown方法SQL查询时,如下所示:SELECT * FROM tbl_Town,我的代码有选择地带来了城镇,但当我点击DrpDwnLstTown时,我看到所有城镇。问题是我必须看到这个城市与城市相连。

0 个答案:

没有答案