异常详细信息:System.FormatException:输入字符串的格式不正确

时间:2012-02-16 07:55:12

标签: c#

我正在根据其他下拉列表填充下拉列表。 当我在第一个下拉列表中选择一个值时,我不断收到此错误..

有人可以向我解释..这是代码..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FillCountry();
    }
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    int CountryID = Convert.ToInt32(ddlCountry.SelectedValue.ToString());
    FillStates(CountryID);
    ddlCity.SelectedIndex = 0;
}
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
    int StateID = Convert.ToInt32(ddlState.SelectedValue.ToString());
    FillCities(StateID);
}

private void FillCountry()
{
    string strConn =      ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT CountryID, CountryName FROM Country";
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();
    if (objDs.Tables[0].Rows.Count > 0)
    {
        ddlCountry.DataSource = objDs.Tables[0];
        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryID";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, "--Select--");
    }
    else
    {
        lblMsg.Text = "No Countries found";
    }


}

private void FillStates(int countryID)
{
    string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT StateID, StateName FROM State WHERE CountryID =@CountryID";
    cmd.Parameters.AddWithValue("@CountryID", countryID);
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();
    if (objDs.Tables[0].Rows.Count > 0)
    {
        ddlState.DataSource = objDs.Tables[0];
        ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "StateID";
        ddlState.DataBind();
        ddlState.Items.Insert(0, "--Select--");
    }
    else
    {
        lblMsg.Text = "No states found";
    }
}

private void FillCities(int stateID)
{
    string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT CityID, CityName FROM City WHERE StateID =@StateID";
    cmd.Parameters.AddWithValue("@StateID", stateID);
    DataSet objDs = new DataSet();
    SqlDataAdapter dAdapter = new SqlDataAdapter();
    dAdapter.SelectCommand = cmd;
    con.Open();
    dAdapter.Fill(objDs);
    con.Close();
    if (objDs.Tables[0].Rows.Count > 0)
    {
        ddlCity.DataSource = objDs.Tables[0];
        ddlCity.DataTextField = "CityName";
        ddlCity.DataValueField = "CItyID";
        ddlCity.DataBind();
        ddlCity.Items.Insert(0, "--Select--");
    }
    else
    {
        lblMsg.Text = "No Cities found";
    }
}
}

我认为我的问题就在这里......

int CountryID = Convert.ToInt32(ddlCountry.SelectedValue.ToString());

即时为我的所有表使用varchar数据类型..

2 个答案:

答案 0 :(得分:3)

大概下拉列表值不是整数的字符串表示。

虽然可能使用int.TryParse或(更糟糕的)try / catch块,但可能SelectedValue选项是控制的选项而不是用户,所以他们 所有都适用于你将要做的任何事情。

要做的第一件事可能是在调试器中查看 - 当你得到一个异常(应该使调试器中断)时,你试图转换的值是多少?一旦你获得了“坏价值”,就要弄清楚它是如何到达那里的。

编辑:如果您的值为“PRC0001”,那么显然不是整数。如果您在所有表格中使用varchar,那么您的CountryID字段可能一个varchar - 那么为什么您要尝试首先转换为整数?

答案 1 :(得分:-1)

如果ddlCountry.SelectedValue.ToString()的值为非数字或空,则会发生此错误。试着把它放在try catch块中。