我正在調用一種方法來填充 asp.net 網絡表單中的下拉菜單。該方法調用檢索名稱列表的存儲過程。它還從數據庫中檢索成員 ID。所有這一切都很好,但是,我需要成員 ID 字段隨著下拉菜單中的每個關聯名稱而更改。例如,我從下拉列表中選擇名稱 Barney Fife,它返回成員 ID '1'。問題是當我從下拉列表中選擇不同的名稱時 - 會員 ID 沒有改變。這是我檢索數據的 C# 方法: public void GetMemberName() { try { using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("dbo.spGetMemberNames", con)) { con.Open(); using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { sda.Fill(ds); // ds is declared in the scope. cmbNames.DataSource = ds; cmbNames.DataTextField = "FormattedName"; cmbNames.DataValueField = "FormattedName"; cmbNames.DataBind(); } using (SqlDataReader read = cmd.ExecuteReader()) { while (read.Read()) { lblMemberID.Text = (read["MemberID"].ToString()); // Places the member ID associated with the name in a label. } } } } } catch (Exception ex) { lblError.Text = ex.ToString(); } }這是我的存儲過程:CREATE PROCEDURE [dbo].[spGetMemberNames]ASBEGIN SELECT FirstName + ' ' + LastName [FormattedName],MemberID FROM tblMembers ORDER BY LastNameEND我是否需要調用一個完全不同的方法和存儲過程來連接到數據庫并根據 DropDown SelectedIndexChange 上的 FormattedName(來自我的存儲過程)查找成員 ID?
- 2 回答
- 0 關注
- 155 瀏覽
添加回答
舉報
0/150
提交
取消