2017-09-16 19 views
0

我面臨一個小問題,我會告訴你下面的所有細節。如果你幫助我,我會很開心。我不能從數據庫中的數據DropDownList選擇性(城市的東西,System.NullReferenceException錯誤)

我在我的數據庫中有3個表格,分別爲「tbl_User」,「tbl_City」,「tbl_Town」。

我 「tbl_User」 表:

  • 用戶ID INT [PK],
  • 電子郵件爲nvarchar(50),
  • 密碼爲nvarchar(50),
  • 城市INT,
  • 鎮int

我的「tbl_City」表:

  • cityno INT [PK],
  • 城市名爲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中選擇的,但程序沒有看到選定的城市。

我的代碼如下:

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時,我看到了所有的城鎮。問題是我只能看到連接城市的城鎮。

+0

我上面寫道,但它是DrpDwnLstTown.Items。FindByValue(dr [2] .ToString())。Selected = true;線。 – Shadouspan

回答

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())); 
    } 
+0

我會厭倦你一點,但我應該如何改變GetTown()方法?如果你幫助我,我會很感激。 – Shadouspan

+0

是的..請給我幾分鐘 –

+0

請檢查現在添加的完整代碼。你不需要GetCityAndTownSelectively()。如果有什麼不清楚的,請告訴我 –

相關問題