简体   繁体   中英

How to add two column values to a list

How can I add two column value to a list, the two column are in different table. email is in tbl_user table and groupname is in tbl_group table. And I want to use the prefix text for both the email and group name for the auto complete text box. Now with the query I am able to add the email id to the list and when I type arr then email id starting with arr are displayed how can I do it for both groupname and emailID

  public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
           MySqlCommand command = new MySqlCommand();


    string textforlist = "SELECT tbl_user.Email from tbl_user WHERE tbl_user.OrganisationID = '1' AND UserActive='Yes'AND tbl_User.Email like '" + prefixText + "%'";
    DataSet ds = new DataSet();
    MySqlParameter[] param = new MySqlParameter[1];
    param[0] = new MySqlParameter("@OrganisationID", MySqlDbType.Int32);
    param[0].Value = count;
    command.Parameters.AddWithValue("@OrganisationID", count);
    using (DataServer server = new DataServer())
    {

        ds = server.ExecuteQuery(CommandType.Text, textforlist, param);

    }
    string textforlist1 = "SELECT tbl_group.GroupName from tbl_group WHERE tbl_group.OrganisationID = '1' AND GroupActive='Yes'AND tbl_group.GroupName like '" + prefixText + "%'";
    DataSet ds1 = new DataSet();
    using (DataServer server = new DataServer())
    {

        ds1 = server.ExecuteQuery(CommandType.Text, textforlist1, param);

    }
    List<string> cityList = new List<string>();
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        cityList.Add(ds.Tables[0].Rows[i][0].ToString());
        cityList.Add(ds1.Tables[0].Rows[i][0].ToString());
    }
    return cityList.ToArray();
}

Just add the other column value as well? Maybe I don't understand the question correctly but this should be straightforward - your table has two columns, Email is the first and GroupName the second:

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
   cityList.Add(ds.Tables[0].Rows[i][0].ToString());
   cityList.Add(ds.Tables[0].Rows[i][1].ToString());
}

You can modify your sql query to restrict the returned group names similar to how you currently do it for Email - you really should use SQL parameters for this though, otherwise you are open for SQL injection attacks.

Edit:

With your updated code you should use two for loops

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    cityList.Add(ds.Tables[0].Rows[i][0].ToString());
}

for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
    cityList.Add(ds1.Tables[0].Rows[i][0].ToString());
}

In case your table column selection increase then you will be requiring List filled with Custom objects. To achieve you must create a custom object class first.

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