InvalidCastException,未被用户代码处理

时间:2014-07-29 14:29:59

标签: c# asp.net sql

我的代码被抓到了,我不确定原因:

int newPersonID = (int)cmd.ExecuteScalar();

它说

  

指定的演员表无效。

任何人都可以帮我解决这个错误吗?我的PersonID在数据库表中是int类型。

protected void AddNewCustomer(object sender, EventArgs e)
{
    string nFirstName = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
    string nLastName = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
    string nEmergency = ((TextBox)GridView1.FooterRow.FindControl("txtEmergency")).Text;
    string nCell = ((TextBox)GridView1.FooterRow.FindControl("txtCell")).Text;
    string nAge = ((TextBox)GridView1.FooterRow.FindControl("txtAge")).Text;
    string nActivityCard = ((TextBox)GridView1.FooterRow.FindControl("txtActivityCard")).Text;
    string nInitials = ((TextBox)GridView1.FooterRow.FindControl("txtInitials")).Text;
    string nBoat = ((TextBox)GridView1.FooterRow.FindControl("txtBoat")).Text;
    string nGroup = ((TextBox)GridView1.FooterRow.FindControl("txtGroup")).Text;

    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand("INSERT INTO Person(FirstName, LastName, Emergency#, Cell#, Age, ActivityCard, CraftType, Initials, Group#) " +
    "values(@FirstName, @LastName, @Emergency, @Cell, @Age, @ActivityCard, @Boat, @Initials, @Group);" +
                      "SELECT SCOPE_IDENTITY();");

    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = nFirstName;
    cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = nLastName;
    cmd.Parameters.Add("@Emergency", SqlDbType.NChar).Value = nEmergency;
    cmd.Parameters.Add("@Cell", SqlDbType.NChar).Value = nCell;
    cmd.Parameters.Add("@Age", SqlDbType.NChar).Value = nAge;
    cmd.Parameters.Add("@ActivityCard", SqlDbType.NChar).Value = nActivityCard;
    cmd.Parameters.Add("@Initials", SqlDbType.NChar).Value = nInitials;
    cmd.Parameters.Add("@Boat", SqlDbType.VarChar).Value = nBoat;
    cmd.Parameters.Add("@Group", SqlDbType.VarChar).Value = nGroup;

    cmd.Connection = con;
    con.Open();

    int newPersonID = (int)cmd.ExecuteScalar();
    con.Close();

    SqlCommand cmd1 = new SqlCommand();
    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText ="insert into TripSchedule(TripType, PersonID, Time, Date) values ('" + ddlTripType.SelectedItem + "', " + newPersonID + "', '" + ddlTripTime.SelectedItem + "', '" + TextBox1.Text + "');" +
"SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
    "Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
    "FROM Person INNER JOIN " +
    "TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = '" + TextBox1.Text + "' and " +
    "TripSchedule.Time = '" + ddlTripTime.SelectedItem + "' and TripSchedule.TripType = '" + ddlTripType.SelectedItem + "';";
    GridView1.DataSource = GetData(cmd);
    GridView1.DataBind();
}

1 个答案:

答案 0 :(得分:1)

Convert.ToInt32(cmd.ExecuteScalar());

编辑:那是对的,在那个问题主题中查看答案。