简体   繁体   English

无法将数组列表转换为字符串

[英]Cannot convert array list to string

I had Repeater which bind method this method retrieve data from data base by stored procedure when Model_Id pass to this method it retrieve data also user can repeat repeater more than once when user select DDL more than once.I did the code and I add all Model_Ids which user select from DDL in array list but error appear 我有Repeater绑定方法,当Model_Id传递给该方法时,该方法通过存储过程从数据库中检索数据,它还会检索数据;当用户多次选择DDL时,用户也可以重复转发器不止一次。我做了代码,并添加了所有Model_Ids哪个用户从数组列表中的DDL中选择但出现错误

Cannot convert array list to string value: 无法将数组列表转换为字符串值:

protected void Add_Click(object sender, ImageClickEventArgs e)
{
    ArrayList Array = new ArrayList();
    Array.Add(DDLModel.SelectedValue);
    DLHome.DataSource = Cls.GetModelName(Array);
    DLHome.DataBind();
}

public DataTable GetModelName(string Model_Id)
{
    using (SqlConnection conn = Connection.GetConnection())
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetComparisonModel";
        SqlParameter ParentID_Param = cmd.Parameters.Add("@Model_Id", SqlDbType.Int);
        ParentID_Param.Value = Model_Id;
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        da.Fill(dt);
        return dt;
    }
}

In addition to what others have said about the datatype mismatch between the ArrayList and the string in the method signature you don't look like you actually want it to be a string anyway since you are passing it as an int parameter to the stored procedure. 除了其他人所说的关于ArrayList和方法签名中的字符串之间的数据类型不匹配之外,您似乎也不希望实际上是字符串,因为您将其作为int参数传递给了存储过程。

I'd suggest in the first method you want to do an int.Parse (or int.TryParse as you see fit) and pass it to the method as an int. 我建议在第一种方法中,您要执行一个int.Parse(或您认为合适的int.TryParse)并将其作为int传递给该方法。

The error is here: 错误在这里:

ArrayList Array = new ArrayList(); // Declare Array of type ArrayList
Array.Add(DDLModel.SelectedValue);
DLHome.DataSource = Cls.GetModelName(Array); // Pass Array to method that expects a string

You are trying to pass an ArrayList into the method GetModelName . 您正在尝试将ArrayList传递给方法GetModelName This method expects a string parameter. 此方法需要一个string参数。

Your method public DataTable GetModelName(string Model_Id) expects a string as defined in the signature. 您的方法public DataTable GetModelName(string Model_Id)需要一个在签名中定义的字符串。

You are trying to pass an ArrayList into the method - Cls.GetModelName(Array); 您正在尝试将ArrayList传递给方法Cls.GetModelName(Array);

You should be getting warned about this in the IDE (and about the variable named Array ) 您应该在IDE中对此有所警告(以及有关名为Array的变量)的警告。

Surely you are getting a compiler error if this is you exact code? 如果这是确切的代码,您肯定会遇到编译器错误吗? GetModelName takes a string as the parameter and you are passing in type ArrayList . GetModelName将字符串作为参数,并且您要传入ArrayList类型。

If you want to convert your array of ID's to string just have an extension method of ArrayList which iterates over each item and returns the string value eg 如果要将ID数组转换为字符串,只需使用ArrayList的扩展方法,该方法将遍历每个项目并返回字符串值,例如

public static string ToArrayString(this ArrayList list)
{
    return String.Join(", ", Cast<string>().ToArray());
}

Looking at your code, the ArrayList usage appears negligiable as you can simply pass SelectedValue straight into the Bind method. 查看您的代码,由于您可以直接将SelectedValue直接传递给Bind方法,因此ArrayList用法似乎可以忽略不计。

btw, your code could look like this: 顺便说一句,您的代码可能如下所示:

using (SqlConnection conn = Connection.GetConnection())
using (SqlCommand cmd = conn.CreateCommand())
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "GetComparisonModel";
    cmd.Parameters.Add("@Model_Id", SqlDbType.Int).Value = Model_Id;
    DataTable dt = new DataTable();
    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
    {
        da.Fill(dt);
    }
    return dt;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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