使用文本框将日期传递到数据表 [英] passing date to data table using textbox

查看:73
本文介绍了使用文本框将日期传递到数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次提醒大家,我试图将日期值从文本框中传递到数据表中,

不断抛出异常,这是我的代码

hey again guys, im trying to pass a date value from a text box into a data table,

keeps throwing exception tho, this is my code

protected void insertButton2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TravelShopConnectionString1"].ConnectionString);
            SqlCommand insertHoliday = new SqlCommand("INSERT INTO Holiday (Continent, Title, [Departure Date], [Return Date], Description) VALUES (@continent, @title, @departureDate, @returnDate, @description)", conn);
            insertHoliday.CommandType = CommandType.Text;

            insertHoliday.Parameters.AddWithValue("@continent", holidayContinentTB.Text);
            insertHoliday.Parameters.AddWithValue("@title", holidayTitleTB.Text);
            insertHoliday.Parameters.AddWithValue("@departureDate", holidayDeparturDTB.Text.ToString());
            insertHoliday.Parameters.AddWithValue("@returnDate", holidayReturnDTB.Text.ToString());
            insertHoliday.Parameters.AddWithValue("@description", holidayDescriptionTB.Text);

            try
            {
                conn.Open();
                insertHoliday.ExecuteNonQuery();
                Response.Redirect("AdminRegistered.aspx");
            }
            catch (Exception)
            {
                Response.Write("<b>Failed please try again.</b>");
            }
        }




它不会让我将其作为字符串传递,但是在我以前的代码中,我可以将int作为字符串传递回,请问有什么想法吗?

我在文本框中输入日期,例如-21/06/2012

我是否输入了错误的代码,因为如果我通过sql manager直接写到表中,似乎会用到它

任何帮助,不胜感激
关于Matt.




it wont let me pass it as a string, but in my previouse code i can pass an int back as a string, any ideas please?

i am typing in text box date like - 21/06/2012

am i typing it in wrong, as it seems to take it if i write directly to the table through sql manager

any help much appreciated
Regards Matt.

推荐答案

默认设置适用于美国英语,因此您的日期应采用mm/dd/yyyy格式.

您还应该了解有关使用块的信息.使用SEH很好,但是在处理程序中,您没有关闭或处置连接或命令.您不应该仅仅依赖于退出范围.
The default setting are for US English so you date should be in the format mm/dd/yyyy.

You should also learn about using blocks. Its good you are using SEH but in the handler you are not closing or disposing of your connection or command. You should not simply rely on exiting scope.


这篇关于使用文本框将日期传递到数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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