Reputation: 1899
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
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;
}
}
Upvotes: 0
Views: 940
Reputation: 100258
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;
}
Upvotes: 0
Reputation: 27599
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.
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.
Upvotes: 1
Reputation: 82096
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
.
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 e.g.
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.
Upvotes: 0
Reputation: 14391
Your method public DataTable GetModelName(string Model_Id)
expects a string as defined in the signature.
You are trying to pass an ArrayList into the method - Cls.GetModelName(Array);
You should be getting warned about this in the IDE (and about the variable named Array
)
Upvotes: 0
Reputation: 32094
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
. This method expects a string
parameter.
Upvotes: 0