无法将数据插入表中

时间:2014-01-29 11:01:21

标签: c#-4.0

`protected void Page_Load(object sender, EventArgs e)
    {
        TextBox6.Text = DateTime.Now.ToShortDateString();
        TextBox7.Text = DateTime.Now.AddMonths(6).ToShortDateString();
    }` protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=ASHISH;Initial Catalog=esaote;Integrated Security=True");
        string q = "insert into info(c_name,c_address, machine, s_version, email,i_ date,due_date) values(@c_name, @c_address, @machine, @s_version, @email, @i_date,@due_date)";
        SqlCommand cmd = new SqlCommand(q, con);

        cmd.Parameters.AddWithValue("@c_name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@c_address", TextBox2.Text);
        cmd.Parameters.AddWithValue("@machine", TextBox3.Text);
        cmd.Parameters.AddWithValue("@s_version", TextBox4.Text);
        cmd.Parameters.AddWithValue("@email", TextBox5.Text);
        cmd.Parameters.AddWithValue("@i_date",TextBox6.Text );
        cmd.Parameters.AddWithValue("@due_date", TextBox7.Text);
        //string due_date = DateTime.Now.ToShortDateString() + DateTime.Now.AddMonths(6).ToShortDateString();
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            Response.Redirect("<script language='javascript'>alert('due date is'" + TextBox7.Text + ")</script>");
        }
        catch (Exception exp)
        {
            Console.Write(exp.Message);
        }
        finally
        {
            con.Close();
        }
    }

catch块在日期异常附近给出了错误的语法。我已将最后一个cmd.parameter中的i_date更改为due_date。表中的所有属性都与given.i相同。我无法找到sol。也转换了语法

1 个答案:

答案 0 :(得分:2)

错误在这里:

插入信息(c_name,c_address,machine,s_version,email, i_ date ,due_date)

应该是:

insert into info(c_name,c_address, machine, s_version, email,i_date,due_date)