简体   繁体   English

ORA-01843:无效月份-在DB上工作,但在asp.net网页上进行相同操作时却无效

[英]ORA-01843: not a valid month — working on DB but not when doing the same on asp.net web pages

I'm trying to insert a record into the DB via asp.net C# web page using 'add button'. 我正在尝试使用“添加按钮”通过asp.net C#网页将记录插入数据库。 The date format on my DB is 'dd/MON/yyyy'. 我的数据库上的日期格式为“ dd / MON / yyyy”。 the isert statement works fine on my DB but it doesnt in asp.net. isert语句在我的数据库上工作正常,但是在asp.net中却没有。

On my DB:: WORKS FINE! 在我的DB上::很好!

INSERT INTO EVENT (RES_ID,EMP_ID,PHONE_NUMBER,EVENT_DT,TIME_SLOT)  
values (null,100,'123-123-1233','01/Oct/2012','08:00 PM - 12:00 AM');

Om myaspx.cs page:: THROWS THE ERROR ""Error inserting record! OM myaspx.cs页面::引发错误“”插入记录时出错! ORA-01843: not a valid month"" on the webpage. ORA-01843:网页上的有效月份“”无效。

    string insertSQL;
    insertSQL = "insert into event (res_id,emp_id,phone_number,event_dt,time_slot) ";
    insertSQL += " values (:res_id,:emp_id,:phone_number,:event_dt,:time_slot)";

    OracleConnection con = new OracleConnection(connectionString);
    OracleCommand cmd = new OracleCommand(insertSQL, con);

    cmd.Parameters.Add(":emp_id", cboResOrEmpName.SelectedValue);
    cmd.Parameters.Add(":res_id", null);
    cmd.Parameters.Add(":phone_number", txtContactNo.Text);
    cmd.Parameters.Add(":time_slot", rblTimeSlot.Text);
    cmd.Parameters.Add(":event_dt", txtEvtDt.Text);

    // Try to open the database and execute the update.
    int added = 0;
    try
    {
        con.Open();
        added = cmd.ExecuteNonQuery();
        lblResults.Text = added.ToString() + " record added!";
    }
    catch (Exception err)
    {
        lblResults.Text += "Error inserting record! ";
        lblResults.Text += err.Message;
    }
    finally
    {
        con.Close();
    }

I used a textbox for the event date with AJAX caledar extansion. 我使用带有AJAX日历扩展的事件日期文本框。 Following is its definition. 以下是其定义。

<asp:TextBox ID="txtEvtDt" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="txtEvtDt_CalendarExtender" runat="server" 
TargetControlID="txtEvtDt" Format="dd/MMM/yyyy">
</asp:CalendarExtender>

Please help me understand the problem. 请帮助我理解问题。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thank you! 谢谢!

希望这会起作用

cmd.Parameters.Add(":event_dt", DateTime.ParseExact(txtEvtDt.Text, "dd/MMM/yyyy", CultureInfo.InvariantCulture));

Your SQL statement works fine on your database presumably because DD/Mon/YYYY is the default date format for whatever program you use to connect to Oracle (SQL*Plus, SQL Developer, TOAD, PL/SQL Developer, etc.) Presumably when ASP.NET connects to the database it doesn't use the same default date format. 您的SQL语句可以在数据库上正常运行,大概是因为DD/Mon/YYYY是用于连接到Oracle的任何程序(SQL * Plus,SQL Developer,TOAD,PL / SQL Developer等)的默认日期格式。 .NET连接到数据库,它不使用相同的默认日期格式。

I'd recommend not relying on automatic conversion between dates and strings. 我建议不要依赖日期和字符串之间的自动转换。 If you're converting between dates and strings, always be explicit about it. 如果要在日期和字符串之间进行转换,请始终对其明确。 Use TO_DATE(some_string, 'DD/Mon/YYYY') to convert a string to a date, and TO_CHAR(some_date, 'DD/Mon/YYYY') to convert a date to a string. 使用TO_DATE(some_string, 'DD/Mon/YYYY')将字符串转换为日期,并使用TO_CHAR(some_date, 'DD/Mon/YYYY')将日期转换为字符串。

So, in your insertSQL string, I'd recommend replacing :event_dt with TO_DATE(:event_dt, 'DD/Mon/YYYY') . 因此,在您的insertSQL字符串中,建议您将:event_dt替换为TO_DATE(:event_dt, 'DD/Mon/YYYY')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在SELECT中计算日期时得到“ ORA-01843:无效的月份” - Getting “ORA-01843: not a valid month” when calculating dates in SELECT 如何解决SQL错误:ORA-01843:无效月份 - How to solve SQL error: ORA-01843: not a valid month ORA-01843:无效月份-在第二台PC上发行 - ORA-01843: not a valid month - issue on second pc 插入表中会返回ORA-01843:无效月份 - insert in to table gives back ORA-01843: not a valid month Oracle.ManagedDataAccess.Client.OracleException:“ORA-01843:无效月份” - Oracle.ManagedDataAccess.Client.OracleException: "ORA-01843: not a valid month" 为什么将datetime转换为字符串会导致错误:ORA-01843:无效的月份 - Why converting datetime to string gets me the error : ORA-01843: not a valid month 如何将字符串类型查询的特定部分转换为最新日期,以避免出现“ ORA-01843:无效月份”错误? - How can I cast specific part of string type query to date to avoid “ORA-01843: not a valid month” error? 当我尝试向Oracle插入日期和时间时得到ORA-01843 - got ORA-01843 when I try to insert date & time to Oracle 同一网站中的ASP.NET WebAPI和Razor页面 - ASP.NET WebAPI and Razor Pages in the same web location Daterangepicker 在 Web 页面 ASP.NET MVC 中不起作用 - Daterangepicker is not working in Web Pages ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM