无法从下拉列表中获取价值 [英] Trouble getting value from dropdownlist

查看:79
本文介绍了无法从下拉列表中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向数据库添加值时遇到问题。当按下按钮时,我收到一条错误,上面写着

I am having trouble with adding values to the database. When pressing on the button I get an error saying

: Input string was not in a correct format

。我已经检查过,sql server中的数据类型与代码中的whats匹配。错误发生在

. I have checked and the datatypes in sql server do match with whats in the code. The error is at the

m.venueID = Convert.ToInt32(ddlvenue.SelectedValue);

行。



MEETING课程如下:

line.

The MEETING class is as follows:

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();
    }


}

推荐答案

无论是什么 ddlvenue.SelectedValue无法转换为int,因此ToInt32失败。我们不知道SelectedValue中有什么,我们不知道所期望的行为是什么,所以我们无法真正提供解决方案。如果SelectedValue可能是非int,则使用int.TryParse而不是ToInt32,因为如果无法转换该值将允许。如果值只应该是int,那么代码中的某些内容在某处是错误的,再次根据您发布的内容,这是不可能的。



学习使用调试器,以便您可以单步调试代码并检查变量值,以了解正在发生的事情。
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.


这篇关于无法从下拉列表中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆