简体   繁体   中英

Retrieve data for List box

I have inserted data from list box. I want to retrieve data for specific skill set in gridview. for example if i select android in dropdownlist and press button it must give people who have android skill set only. Does any body know how to do this

  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);

} 

have your gridview datasource configured, during configuration whereever you have your SkillsetID or whateever that is, FOR THAT, use the "WHERE" clause which say that SkillSetID is bound to the "control" with ID lstBasePackageAsset with "selected value" or just "selected" ie gridview1.skillsetID = lstBasePackageAsset.selectedvalue ;

You will be done.

And please next time, or even now, let people know if it is VisualStudio query or any other developement source query. It is more easier in VS but require more programming code in command line (if you are doing that)

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

是的,你可以使用这样的where子句来做到这一点

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM