Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I am having trouble with adding values to the database. When pressing on the button I get an error saying
C#
: Input string was not in a correct format
. I have checked and the datatypes in sql server do match with whats in the code. The error is at the
C#
m.venueID = Convert.ToInt32(ddlvenue.SelectedValue);
line.

The MEETING class is as follows:
C#
public class MEETING
{
        private int MeetingID;
        private int VenueID;
        private string Venue;
        private string MeetingName;
        private DateTime MeetingStartDate;
        private DateTime MeetingEndDate;

        public int meetingID 
        {
            get { return MeetingID; }
            set { MeetingID=value;}
        }
        public int venueID 
        {
            get { return VenueID; }
            set { VenueID=value;}
        }
        public string meetingName 
        {
            get { return MeetingName; }
            set { MeetingName=value;}
        }
        public DateTime meetingStartDate
        {
            get { return MeetingStartDate; }
            set { MeetingStartDate=value;}
        }
        public DateTime meetingEndDate 
        {
            get { return MeetingEndDate;}
            set { MeetingEndDate=value;}
        }
        public string venue
        {
            get { return Venue; }
            set { Venue = value; }
        }
    }


What I have tried:

protected void btnMeetings_Click(object sender, EventArgs e)
{
    MEETING m = new MEETING();

    try
    {
        m.meetingName = tbMeetingName.Text.ToString();
        m.venueID = Convert.ToInt32(ddlvenue.SelectedValue);
        m.meetingStartDate = Convert.ToDateTime(datePickerStart.Text); ;
        m.meetingEndDate = Convert.ToDateTime(datePickerEnd.Text);


        BusinessLogicLayer bl = new BusinessLogicLayer();
        try
        {
            //Popup label onga
            lblResult.Visible = true;
            lblResult.Text = "Added Succesfully";
            if (bl.AddMeeting(m) == true)
            {
                lblResult.Text = "Meeting was added successfully";

                Response.Redirect("WebForm14.aspx");
            }
        }
        catch (Exception exc)
        {
            lblResult.Visible = true;
            lblResult.Text = "";
            lblResult.Text = lblResult.Text + exc.ToString();
        }
    }
    catch (Exception exc)
    {
        lblResult.Visible = true;
        lblResult.Text = "";
        lblResult.Text = lblResult.Text + exc.ToString();
    }


}
Posted
Updated 2-Dec-16 4:00am
v2

1 solution

Whatever is in "ddlvenue.SelectedValue" can't be converted to an int so ToInt32 is failing. We don't know what is in SelectedValue and we don't know what the desired behaviour is so we can't really offer a solution. If it's possible for SelectedValue to be a non-int then use int.TryParse rather than ToInt32 as that will allow for if the value can't be converted. If the value should only ever be an int then something in your code is wrong somewhere, again from what you've posted it's impossible to tell.

Learn to use the debugger so you can step through your code and inspect variable values to get an idea what is going on.
 
Share this answer
 
Comments
KevinClaassens 4-Dec-16 13:15pm    
Thank you so much for your help. I figured where out where I went wrong

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900