Cannot convert string to int, must be simple but i m missing somewhere
here, i want to check only if the column Active is Yes, then get into the if loop. But it gives me an error "Cannot convert from string to int" for the last condition in if. What do you guys i can do. Thanks!!
if (ds != null && d开发者_开发知识库s.Tables != null
&& ds.Tables.Count > 0
&& ds.Tables[0].Rows.Count > 0
&& ds.Tables[0].Columns[0].ColumnName["Status"] == "Y")
{
disableloc.DataSource = ds;
disableloc.DataBind();
}
else
{
ds = null;
disableloc.DataSource = ds;
disableloc.DataBind();
The stored procedure is SELECT ML.locationname, rtrim(ML.address) + (CASE WHEN ML.Address2 IS NOT NULL THEN ('' '' + rtrim(ML.Address2)) ELSE '''' END) + '' - ''+ ML.city + '', ''+ ML.state as address, ML.locationid, ML.merchantid, case when ML.active <> ''Y'' then ''Deactive'' else ''Active'' end [Status], (SELECT count() as retval FROM merchant_statistics WHERE type = ''merchant'' AND locationID= ML.LocationID AND status = ''clicked'') as stat, '' '' as button,'' '' as blank , dbo.GetCouponCountForLocations(@_merchantid,ML.locationID) couponCount, MP.DomainName, (SELECT Count() FROM Promotion WHERE LocationId = ML.locationid AND PostType = 1) AS jobs FROM merchant_location ML , Merchant_Pages MP WHERE MP.LocationID = ML.LocationID AND ML.merchantid = @_merchantid Order By '
Your compilation error is caused because ColumnName
returns a String, and the only indexer String defines is an integer indexer that returns the character at the specified position.
Assuming you are trying to access the value of the "Active" column for the first row:
ds.Tables[0].Columns[0].ColumnName["Active"] == "Y"
should be
ds.Tables[0].Rows[0]["Active"] == "Y"
ColumnName actually return the name of the data column, in your case , ds.Tables[0].Columns[0].ColumnName return the name of the first column. It's a string, so you can get one of the chars in the string using [] operator. But you have to pass an int, instead of string "Active", so you get the error. I guess you mean to check the column value instead. In the case, you have to use
ds.Tables[0].Rows[0]["Active"]== "Y"
Look at ds.Tables[0].Columns[0].ColumnName["Active"]
You are trying to specify the column twice, one time too many.
Replace it with
ds.Tables[0].Columns[0] == "Y"
or ds.Tables[0].Columns["Active"] == "Y"
ds.Tables[0].Columns[0].ColumnName[should be an INT here]
This returns a character at that passed in index of that column FYI.
I would assume that ds.Tables[0].Columns[0].ColumnName["Active"] returns an int and not a string.
Edit: While my answer was technically correct (the returned value was an int), it was not the desired answer. My apologies. As other posters have mentioned, you need to get the value from the row, not the column.
精彩评论