2013-02-01 37 views
0

我已經從列表框中插入數據。我想檢索gridview中特定技能集的數據。 例如,如果我在dropdownlist中選擇android並按下按鈕,它必須給予只有android技能組的人。 是否有任何身體知道如何做到這一點檢索列表框的數據

private string GetConnectionString() 

{ 

    //Where DBConnection is the connetion string that was set up in the web config file 

    return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; 

} 



private void InsertRecords(StringCollection sc) 

{ 

    SqlConnection conn = new SqlConnection(GetConnectionString()); 

    StringBuilder sb = new StringBuilder(string.Empty); 

    foreach (string item in sc) 

    { 

     const string sqlStatement = "INSERT INTO Table1 (Employees) VALUES"; 

     sb.AppendFormat("{0}('{1}'); ", sqlStatement, item); 

    } 



    try 

    { 

     conn.Open(); 

     SqlCommand cmd = new SqlCommand(sb.ToString(), conn); 

     cmd.CommandType = CommandType.Text; 

     cmd.ExecuteNonQuery(); 

     Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true); 

    } 



    catch (System.Data.SqlClient.SqlException ex) 

    { 

     string msg = "Insert Error:"; 

     msg += ex.Message; 

     throw new Exception(msg); 

    } 

    finally 

    { 

     conn.Close(); 

    } 

} 



protected void Page_Load(object sender, EventArgs e) 

{ 



} 

protected void Button1_Click(object sender, EventArgs e) 

{ 

    StringCollection sc = new StringCollection(); 



    foreach (ListItem item in ListBox1.Items) 

    { 

     if (item.Selected) 

     { 

      sc.Add(item.Text); 

     } 

    } 

    InsertRecords(sc); 

} 
+0

你應該接受一些答案兄弟它有助於社區。問候。 –

回答

1

有你的gridview的數據源配置,配置過程中徘徊無論你有你的SkillsetID或whateever也就是說,對於這一點,使用「WHERE」條款,說SkillSetID勢必ID爲lstBasePackageAsset的「控制」帶有「selected value」或者「selected」,即 gridview1.skillsetID = lstBasePackageAsset.selectedvalue;

你會完成。

請下一次或甚至現在讓人們知道它是否是VisualStudio查詢或任何其他開發源查詢。它是在VS更容易,但在命令行中需要更多的編程代碼(如果你正在做的)

string query="select * from Employees where skill= '" +yourvalue+ "'"; 
1

是的,你可以做到這一點使用where條款這樣

string query="select * from Employees where skill= '" +yourvalue+ "'"; 
+0

這個查詢不是安全的** SQL注入**的機會。 –