Thomas
Thomas

Reputation: 1126

error while retrieving data from database on windows form application

trying to retrieve some data from the database and put in into my form. the code goes

notificationList.Add(new notificationForm(
            (String)dataset.Tables["alertdetails"].Rows[0]["memberName"],
            (String)dataset.Tables["alertdetails"].Rows[0]["locationName"],
            (String)dataset.Tables["alertdetails"].Rows[0]["photo"],
            "",
            (String)dataset.Tables["alertdetails"].Rows[0]["memberid"], 
            "",
            "", x, y, alertId));

the arguments for my notificationForm is

public notificationForm(String name, String location, String imageExtension,
                        String alertType,String memberid,String date,String time,
                        int x,int y,String alertid)

the error goes

Unable to cast object of type 'System.Int32' to type 'System.String'.

on the line whereby i retrieve memberid. anyone know how to fix that? it works fine for memberName, locationName and photo though.

Upvotes: 1

Views: 185

Answers (2)

Stone Cold
Stone Cold

Reputation: 216

dataset.Tables["alertdetails"].Rows[0]["memberid"]

gives you the integer value, if the datatype of memberid in the database is int. Then, either add ToString() at the end or cast it to an int.

Example:

dataset.Tables["alertdetails"].Rows[0]["memberid"].ToString()
                      OR
Convert.ToInt32(dataset.Tables["alertdetails"].Rows[0]["memberid"]) 

Upvotes: 1

Mitch Wheat
Mitch Wheat

Reputation: 300559

Cast it to an int (32bit integer), which is what the error message is telling you.

Check the type of 'memberid' column in the database table. Presumably it is an integer.

Upvotes: 0

Related Questions