检索列表框的数据

时间:2013-02-01 04:55:55

标签: c# asp.net gridview listbox

我已从列表框中插入数据。我想检索gridview中特定技能组的数据。 例如,如果我在下拉列表中选择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);

} 

2 个答案:

答案 0 :(得分:1)

配置你的gridview数据源,在配置期间你有SkillsetID或whateever,那就是,为此,使用“WHERE”子句说SkillSetID绑定到“控制”,ID为lstBasePackageAsset和“selected value”或者只是“选中”即 gridview1.skillsetID = lstBasePackageAsset.selectedvalue;

你将完成。

请下次,甚至现在,让人们知道它是VisualStudio查询还是任何其他开发源查询。它在VS中更容易,但在命令行中需要更多编程代码(如果你这样做)

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

答案 1 :(得分:1)

是的,你可以使用像{/ p>这样的where子句来做到这一点

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