Home > DeveloperSection > Forums > Conversion failed when converting the nvarchar value 'meetingId' to data type int
Hugh Jackman
Hugh Jackman

Total Post:52

Posted on    April-15-2015 7:06 AM

 .NET C# 

 1 Reply(s)
 1230  View(s)
Rate this:

I have a dropdownlist and I am trying to insert the DataValueField into a database table. The DataValueField is retrieved from the database and the data type for the column is INT. However, when the run the query I get this error message


Conversion failed when converting the nvarchar value 'meetingId' to data type int.



if (MinuteUpload.HasFile)


    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;


    string updateSql = "UPDATE Project_Meeting SET meetingMinute = @meetingMinute WHERE meetingId = @MeetingId"; ;


    using (SqlConnection myConnection = new SqlConnection(connectionString))




        SqlCommand myCommand = new SqlCommand(updateSql, myConnection);


        myCommand.Parameters.AddWithValue("@meetingMinute", "postUpload/" + filename);

        myCommand.Parameters.AddWithValue("@MeetingId", MinuteDropDown.DataValueField);







However, when I try



I get this error:


Input string was not in a correct format.

Elena Glibart
Elena Glibart

Total Post:42

Posted on    April-15-2015 7:44 AM

myCommand.Parameters.AddWithValue("@MeetingId", MinuteDropDown.DataValueField);

There are two things wrong. First, DataValueField represents the name of the property, not the value of the property. SelectedValue is what you want.


But once you make that change, SelectedValue is still a string. You should parse that into an int in advance, before adding your parameter:


int meetingId = int.Parse(MinuteDropDown.SelectedValue);

myCommand.Parameters.AddWithValue("@MeetingId", meetingId);

Don't want to miss updates? Please click the below button!

Follow MindStick