转换失败

时间:2016-06-23 09:08:52

标签: asp.net sql-server-2008-r2

我想在表中添加一些参数,我在Visual Studio 2013中收到此错误。我在使用Date&时间。我的日期和时间SQL Server 2008 R2中使用的时间数据类型为DateTime(7)。我收到此错误

  

从字符串

转换日期和/或时间时转换失败

我的错误是什么?

我的代码:

string strquery = "select count(*) from SaloonGeneralReservation where SaloonReservationDate=@SaloonReservationDate";

    SqlConnection connection2 = DBConnection.getConnection();
    connection2.Open();
    SqlCommand cmd2 = new SqlCommand();
    cmd2.Connection = connection2;
    cmd2.CommandText = strquery;

    cmd2.Parameters.Add("@SaloonReservationDate", txt1_date.Text);


    int intRowCount = (int) cmd2.ExecuteScalar();
    txt1_date.Text = intRowCount.ToString();
    connection2.Close();


    if (intRowCount == 1)
    {
        Messages myMsg = new Messages(); // ایجاد نمونه از کلاس
        myMsg.CreateMessageAlert("سالن در این تاریخ رزرو شده است . لطفا تاریخ دیگری وارد نمایید.");
        txt1_date.Text = "";

         }


    else
    {
        string strQuery = "INSERT INTO [WeedingSalonGeneralRes](Date,Time,Fruit,Drink,Desert,MainFood,Salad,TableFlower,SaloonLighting,Saloondesign,SloonCrew,Pastry,GiftCard,customer_id)" + "VALUES(@Date,@Time,@Fruit,@Drink,@Desert,@MainFood,@Salad,@TableFlower,@SaloonLighting,@Saloondesign,@SloonCrew,@Pastry,@GiftCard,@customer_id)";


        SqlConnection connection = DBConnection.getConnection();
        connection.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = connection;
        cmd.CommandText = strQuery;


        string cis = Session["customerID"].ToString();

        cmd.Parameters.Add("@Date", txt1_date.Text);
        cmd.Parameters.Add("@Time", txt2_time.Text);
        cmd.Parameters.Add("@Fruit", fruit);
        cmd.Parameters.Add("@Drink", drink);
        cmd.Parameters.Add("@Desert", desert);
        cmd.Parameters.Add("@MainFood", mainfood);
        cmd.Parameters.Add("@Salad", salad);
        cmd.Parameters.Add("@TableFlower", tableflower);
        cmd.Parameters.Add("@SaloonLighting", saloonlighting);
        cmd.Parameters.Add("@Saloondesign", saloondesing);
        cmd.Parameters.Add("@SloonCrew", salooncrew);
        cmd.Parameters.Add("@Pastry", pastry);
        cmd.Parameters.Add("@GiftCard", giftcard);
        cmd.Parameters.Add("@customer_id", cis);


        cmd.ExecuteNonQuery();
        connection.Close();
        Response.Redirect("ReservationSucces.aspx");
    }    

1 个答案:

答案 0 :(得分:0)

您的TIME(7)至少需要HH:MM:SS,且最多HH:MM:SS.NNNNNNN。因此,在:00的字符串上添加TIME以使转换有效。只要您使用DATEYYYY-MM-DD就可以了。如果您使用的是YYYY-DD-MM,则会收到此错误。