daveredfern
daveredfern

Reputation: 1255

String won't convert to int ready for query

I am trying to convert a field from a string to an int, then pass it to a query using tableadapters.

I have the following function:

protected String countResponses(String value)
{
    String input = value.ToString();
    int fordb = Int32.Parse(input);

    FrontendTableAdapters.ForumTableAdapter ta = new FrontendTableAdapters.ForumTableAdapter();
    DataTable table = ta.CountPosts(value);

    if (table.Rows.Count > 0)
    {
        return table.Rows[0].ItemArray[1].ToString();
    }
    else
    {
    return "Unknown";
    }
}

It is working greate up to putting it in CountPosts() where I get the following error:

Error 4 Cannot implicitly convert type 'object' to 'System.Data.DataTable'. An explicit conversion exists (are you missing a cast?) C:\Users\Dave\Desktop\WebApps\Figmentville\Figmentville\yoursay\Default.aspx.cs 49 31

I think this is because it is looking for an int. But haven't I already converted it to an int?

Thanks

Dave.

Upvotes: 0

Views: 433

Answers (2)

Pharabus
Pharabus

Reputation: 6062

you are passing in value which is the string parameter, you have parsed it into the variable fordb, thats the int you should pass in

edit

also, why do you ToString value when that pararameter is already a string (when you pass the output into the variable input)

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062955

Since we don't know what ForumTableAdapter is, it isn't clear what CountPosts returns, but if this is actually returning a DataTable (but typed as object) it sounds like you just want:

 DataTable table = (DataTable)ta.CountPosts(value);

but then, I also expect Count... to return an int...


Also; minor point: the line String input = value.ToString(); is both unnecessary and potentially a cause of a null bug (but not in this case) - you should be able to just use:

int fordb = int.Parse(value);

(and remove the input variable completely)

Upvotes: 1

Related Questions