2015-09-05 109 views
1

我的項目是在asp.net中的webform1中包含一個文本框和一個保存 按鈕,當用戶在文本框中輸入一些國家名稱時,它應該然後點擊保存按鈕名稱應該被保存在數據庫表中名爲 的國家。 然後在webform2中,保存的國家應該在頁面加載的下拉列表中加載,然後應該有一些文本框,其中用戶將在下拉列表中 輸入所選國家的狀態, 和上次在webform3國家/地區加載下拉列表中的用戶選擇國家/地區時,它應該在另一個 下拉列表中加載該國家的狀態,並且當用戶從第二個下拉列表中選擇一個狀態並且 進入該州的城市時,它應該被保存在數據庫中 點擊保存按鈕。 我有問題,當我運行webform2的國家得到加載在下拉列表1,但是當我選擇一個國家,它不會加載 該國在dropdownlist2國家。 有幫助嗎? 這裏是我的代碼,到目前爲止,ASP.Net C#和微軟SQL連接

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Data; 

namespace assign1 { 
    public partial class city: System.Web.UI.Page { 
     protected void Page_Load(object sender, EventArgs e) { 
      if (IsPostBack == false) { 
       SqlConnection conn = new SqlConnection("Data Source = HAFIZ_HARRON; Database = 7thSemester; Integrated Security = true"); 
       SqlDataAdapter da = new SqlDataAdapter(); 
       da.SelectCommand = new SqlCommand("SELECT CName,CID FROM country", conn); 

       DataTable dt = new DataTable(); 
       da.Fill(dt); 

       ddlCountry.DataSource = dt; 
       ddlCountry.DataTextField = "CName"; 
       ddlCountry.DataValueField = "CID"; 
       ddlCountry.DataBind(); 
      } 


     } 

     protected void btnSave_Click(object sender, EventArgs e) { 
      SqlConnection conn = new SqlConnection("Data Source = HAFIZ_HARRON; Database = 7thSemester; Integrated Security = true"); 

      SqlCommand cmd = new SqlCommand("INSERT INTO city(CIName,SID,CID) Values('" + txtCity.Text + "','" + ddlState.SelectedIndex + "','" + ddlCountry.SelectedIndex + "')", conn); 




      conn.Open(); 
      cmd.ExecuteNonQuery(); 
      conn.Close(); 

     } 

     protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) { 
      /* SqlConnection conn = new SqlConnection("Data Source = HAFIZ_HARRON; Database = 7thSemester; Integrated Security = true"); 
      SqlDataAdapter da = new SqlDataAdapter(); 
      da.SelectCommand = new SqlCommand("SELECT SName,SID FROM state", conn); 

      DataTable dt = new DataTable(); 
      da.Fill(dt); 

      ddlState.DataSource = dt; 
      ddlState.DataTextField = "SName"; 
      ddlState.DataValueField = "SID"; 
      ddlState.DataBind(); */ 

     } 

     protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { 
      SqlConnection conn = new SqlConnection("Data Source = HAFIZ_HARRON; Database = 7thSemester; Integrated Security = true"); 
      SqlDataAdapter da = new SqlDataAdapter(); 
      da.SelectCommand = new SqlCommand("SELECT SName,SID FROM state", conn); 

      DataTable dt = new DataTable(); 
      da.Fill(dt); 

      ddlState.DataSource = dt; 
      ddlState.DataTextField = "SName"; 
      ddlState.DataValueField = "SID"; 
      ddlState.DataBind(); 
     } 
    } 
} 


---------- 
+0

選擇狀態爲特定的國家需要你設定的條件國家=? ??否則你加載你的表中的每個狀態。這是現在發生嗎?表_state_的結構是什麼? – Steve

+0

表狀態我使用了國家表中的stateid,stateName和(countryid) – harron

+0

SqlConnection conn = new SqlConnection(「Data Source = HAFIZ_HARRON; Database = 7thSeter; Integrated Security = true」); SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(「SELECT SName,SID FROM state WHERE CID =」,conn); DataTable dt = new DataTable(); da.Fill(dt); 我如何設置條件的國家da.SelectCommand =新的SqlCommand(「選擇SName,SID從狀態WHERE CID = ???」,康涅狄格州); ??? – harron

回答

0

您可以使用ASP.NET 的AJAX更新面板,將徹底解決問題的 觸發器可以指定所選索引更改事件

2

當您收到ddlCountry_SelectedIndexChanged你需要準備只選擇屬於當前選擇的國家狀態的查詢。

所以,你的代碼應該是

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    using(SqlConnection conn = new SqlConnection(.....)) 
    using(SqlDataAdapter da = new SqlDataAdapter()) 
    { 
     string cmdText = @"SELECT SName,SID FROM state WHERE [email protected]"; 
     da.SelectCommand = new SqlCommand(cmdText, conn); 
     da.SelectCommand.Parameters.Add("@country", SqlDbType.Int).Value = ddlCountry.SelectedValue; 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 
     ddlState.DataSource = dt; 
     ddlState.DataTextField = "SName"; 
     ddlState.DataValueField = "SID"; 
     ddlState.DataBind(); 
    } 
} 

這裏查詢文本沒有內置連接字符串,但使用參數佔位符(WHERE CID = @國家),然後添加具有確切的數據類型所需要的參數(我想是一個整數)到SelectCommand的Parameters集合。

有關使用說明的幾句話。這是使用一次性物品的首選方式,因爲使用塊確保在退出塊時放置物件。

最後,您的連接字符串應該存儲在web.config文件中,否則,如果您需要更改此字符串的任何細節,您必須搜索應用程序中您硬編碼該字符串的每個位置