Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向列表数组添加值_C#_Asp.net_List - Fatal编程技术网

C# 如何向列表数组添加值

C# 如何向列表数组添加值,c#,asp.net,list,C#,Asp.net,List,我正在为国家/地区和城市创建一个级联下拉列表,目前运行良好,但无论用户选择哪个国家/地区,我都需要将其他城市作为最后一个值添加到第二个下拉列表(城市DD)。我无法使它像正常的列表添加操作一样工作 public CascadingDropDownNameValue[] FetchCities(string knownCategoryValues, string category) { string CountryCode; StringDictionary

我正在为国家/地区和城市创建一个级联下拉列表,目前运行良好,但无论用户选择哪个国家/地区,我都需要将
其他城市
作为最后一个值添加到第二个下拉列表(城市DD)。我无法使它像正常的列表添加操作一样工作

public CascadingDropDownNameValue[] FetchCities(string knownCategoryValues, string category)
    {
        string CountryCode;
        StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        CountryCode = strCountries["Country"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con);
        cmd.Parameters.AddWithValue("@Code", CountryCode);
        cmd.ExecuteNonQuery();
        SqlDataAdapter dastate = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dastate.Fill(ds);
        con.Close();
        List<CascadingDropDownNameValue> states = new List<CascadingDropDownNameValue>();
        foreach (DataRow dtRow in ds.Tables[0].Rows)
        {
            string StateID = dtRow["CityID"].ToString();
            string StateName = dtRow["CityName"].ToString();
            states.Add(new CascadingDropDownNameValue(StateName, StateID));
        }
        return states.ToArray();
    }
public CascadingDropDownNameValue[]获取城市(字符串knownCategoryValue,字符串类别)
{
字符串国家代码;
StringDictionary strCountries=AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
CountryCode=strCountries[“Country”]。ToString();
con.Open();
SqlCommand cmd=new-SqlCommand(“选择*来自国家/地区=@Code Order by CityName的城市”,con);
cmd.Parameters.AddWithValue(“@Code”,CountryCode);
cmd.ExecuteNonQuery();
SqlDataAdapter dastate=新的SqlDataAdapter(cmd);
数据集ds=新数据集();
dastate.Fill(ds);
con.Close();
列表状态=新列表();
foreach(ds.Tables[0].行中的DataRow dtRow)
{
字符串StateID=dtRow[“CityID”].ToString();
字符串StateName=dtRow[“CityName”].ToString();
Add(新的CascadingDropDownNameValue(StateName,StateID));
}
返回状态。ToArray();
}

上面的代码是一个示例代码,我在下面介绍级联下拉列表

您可以在不接触代码的情况下执行此操作。。只需将“Cities”新行添加到表中即可

然后更新您的SQL:

SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code OR Country='xxx' Order by CityName ", con);

开头的
ZZZ
只是为了确保当order by CityName

作为返回值将其作为列表传递给函数(我不喜欢返回列表)时,它始终位于末尾。通过这种方式,您可以控制在获取数据库值后列表的变化

public void Fill()
{
    List<CascadingDropDownNameValue> cities = new List<CascadingDropDownNameValue>();

    FetchCities(cities, knownCategoryValues, category);

    cities.Add(new CascadingDropDownNameValue("new city", "0"));
}


public void FetchCities(List<CascadingDropDownNameValue> values, string knownCategoryValues, string category)
{
    string CountryCode;
    StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
    CountryCode = strCountries["Country"].ToString();
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con);
    cmd.Parameters.AddWithValue("@Code", CountryCode);
    cmd.ExecuteNonQuery();
    SqlDataAdapter dastate = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    dastate.Fill(ds);
    con.Close();
    List<CascadingDropDownNameValue> states = new List<CascadingDropDownNameValue>();
    foreach (DataRow dtRow in ds.Tables[0].Rows)
    {
        string StateID = dtRow["CityID"].ToString();
        string StateName = dtRow["CityName"].ToString();
        values.Add(new CascadingDropDownNameValue(StateName, StateID));
    }
}

祝你好运。

问题出在哪里还不清楚。你为什么不把states.Add(新的CascadingDropDownNameValue(“其他城市”,“1”))放在一个
states.Add中或类似的东西后,foreach?我尝试了相同的第一次没有工作的任何方式与您的代码很好地工作。括号结尾处缺少。。。。谢天谢地,我无法理解问题所在。@jeroen,问题已经解决了,我想在列表的末尾追加一个,这是通过使用
states完成的。添加(新的CascadingDropDownNameValue(“其他城市”,“1”)@KnowledgeSeek,我发了一篇帖子,但删除了,但我认为我的方向是对的。看下面(取消删除它)。我的重点是分离数据库代码和手动添加()。
public void Fill()
{
    List<CascadingDropDownNameValue> cities = new List<CascadingDropDownNameValue>();

    FetchCities(cities, knownCategoryValues, category);

    cities.Add(new CascadingDropDownNameValue("new city", "0"));
}


public void FetchCities(List<CascadingDropDownNameValue> values, string knownCategoryValues, string category)
{
    string CountryCode;
    StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
    CountryCode = strCountries["Country"].ToString();
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con);
    cmd.Parameters.AddWithValue("@Code", CountryCode);
    cmd.ExecuteNonQuery();
    SqlDataAdapter dastate = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    dastate.Fill(ds);
    con.Close();
    List<CascadingDropDownNameValue> states = new List<CascadingDropDownNameValue>();
    foreach (DataRow dtRow in ds.Tables[0].Rows)
    {
        string StateID = dtRow["CityID"].ToString();
        string StateName = dtRow["CityName"].ToString();
        values.Add(new CascadingDropDownNameValue(StateName, StateID));
    }
}
    public void Fill()
    {
        List<CascadingDropDownNameValue> cities = new List<CascadingDropDownNameValue>();

        cities.AddRange(FetchCities(connectionString, knownCategoryValues, category));

        cities.Add(new CascadingDropDownNameValue("new city", "0"));
    }


    public IEnumerable<CascadingDropDownNameValue> FetchCities(string connectionString, string knownCategoryValues, string category)
    {
        StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        string CountryCode = strCountries["Country"].ToString();

        using (SqlConnection con = new SqlConnection(connectionString))
        using (SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con))
        {
            cmd.Parameters.Add("@Code", SqlDbType.VarChar).Value = CountryCode;
            using (SqlDataReader reader = cmd.ExecuteReader())
                while (reader.Read())
                {
                    string StateID = reader["CityID"].ToString();
                    string StateName = reader["CityName"].ToString();
                    yield return new CascadingDropDownNameValue(StateName, StateID);
                }
        }
    }
var cities = FetchCities(connectionString, knownCategoryValues, category).Take(10);
cities.AddRange(cities);