获取要插入数据库的日期时出错 [英] error in getting date to be inserted in database

查看:73
本文介绍了获取要插入数据库的日期时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将asp.net和c#与sql-server2005数据库一起使用,以创建一个简单的网页.该网页捕获了两个信息ID和日期.此日期存储在表中,其列为ID(整数类型)和日期(日期时间类型),此处为代码
asp.net

I am using asp.net and c# with sql-server2005 database to create a simple webpage. the said webpage captures two information id as well as date.this date is stored in tables whose columns are id(int type)and date(datetime type) here is code
asp.net

<form id="form1" runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td align="center"> 
                    
                    Enter ID</td>
                <td class="style2">
                    <asp:TextBox ID="txtiD" runat="server" ValidationGroup="submit"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvId" runat="server" ControlToValidate="txtiD" 

                        ErrorMessage="id is must" ValidationGroup="submit">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td align="center"> 
                    
                    Enter Date</td>
                <td class="style2">
                    <asp:TextBox ID="txtdate" runat="server" ValidationGroup="submit"></asp:TextBox>
                    <asp:CompareValidator ID="cmpdate" runat="server" 

                        ControlToValidate="txtdate" ErrorMessage="mm/dd/yy" 

                        Operator="DataTypeCheck" Type="Date" ValidationGroup="submit">*</asp:CompareValidator>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" 

                        ValidationGroup="submit" />
                </td>
                <td align="left">
                    <asp:Label ID="lblresult" runat="server" Text="Label" Visible="False"></asp:Label>
                </td>
            </tr>
        </table>
    
    </div>
    </form>


这是输入数据的C#逻辑


and here is c# logic to enter data

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection conn;
    SqlCommand cmd;
    string str = ConfigurationManager.ConnectionStrings["prsql2ConnectionString"].ConnectionString;
    //DateTime dt;/*carried out this step after reading code project but still same ans*/
     
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            dt = DateTime.Parse(txtdate.Text);
            conn = new SqlConnection(str);
            cmd = new SqlCommand("insert into dtcheck(@idi,@dc)", conn);
            cmd.Parameters.AddWithValue("@idi", txtiD.Text);
            cmd.Parameters.AddWithValue("@dc", dt);
            conn.Open();
            int a = cmd.ExecuteNonQuery();
            if (a > 0)
            {
                lblresult.Visible = true;
                lblresult.Text = "insert operation sucessfull";
            }
        }
        catch (Exception e1)
        {
            lblresult.Visible = true;
            lblresult.Text = e1.Message.ToString() + "and" + e1.Source.ToString();/* message cannot be used as an method*/
            //lblresult.Text = ;
        }
        finally
        {
            if (conn != null)
                conn.Close();
        }
    }
}



但我遇到以下错误
'')''.and.Net SqlClient数据提供程序附近的语法不正确
请帮助我识别错误,并建议一种更好的方法来进行此类操作.



but i am getting following error
Incorrect syntax near '')''.and.Net SqlClient Data Provider
please help me to identify bug and please recommend a better way to carry out such operation

推荐答案

尝试编写正确的Insert语句.
Try to write correct Insert statement.
cmd = new SqlCommand("insert into dtcheck(1stColumnName,2ndColumnName) values(@idi,@dc)", conn);


要酷

在按钮单击中放置断点..
然后调试您的代码,获取查询,将其复制并直接在数据库中执行,然后检查..您的问题将很容易解决
be cool

put break point in button click..
then debug your code get the query copy it and directly execute it in your database then check ..your problem will be solved easily


也请检查您的数据库字段
Also check your database field also


这篇关于获取要插入数据库的日期时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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