我的代码中有两个错误,但无法看到我出错的地方。 [英] I have two errors in my code , but cant see where I am going wrong.

查看:51
本文介绍了我的代码中有两个错误,但无法看到我出错的地方。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,如果有人可以帮助我。我创建了一个预订ASP.NET Web应用程序4.5,并在管理员页面上遇到问题,管理员用户可以在其中创建手动预订。以下是我遇到的两个错误。



1:将nvarchar值'Full'转换为数据类型int时转换失败。

2 :无法将char值转换为smallmoney。 char值的语法不正确。



这里是我的代码,它是令人心烦的。



  protected   void  Button1_Click1( object  sender,EventArgs e)
{
// 制作新的工作记录并插入来自表单的数据 - 如果没有Cist_id则必须以null为单位

SqlCommand cmd = new SqlCommand ( INSERT INTO Job(Date,Start_Time,Valet_ID,Additions,Car_Reg,Worker_ID,Comments,Cust_ID)值(@Date ,@ Start_Time,@ Vale_ID,@ Additions,@ Car_Reg,@ Worker_ID,@ Comments,@ Customer_ID),con);
cmd.Parameters.AddWithValue( @ Date,SqlDbType.Date).Value = txtDate.Text;
cmd.Parameters.AddWithValue( @ Start_Time,SqlDbType.Time).Value = DropTime.Text;
cmd.Parameters.AddWithValue( @ Valet_ID,txtValetType.Text);
cmd.Parameters.AddWithValue( @ Additions,txtAdditions.Text);
cmd.Parameters.AddWithValue( @ Car_Reg,txtCarReg.Text);
cmd.Parameters.AddWithValue( @ Worker_ID,txtWorker_ID.Text);
cmd.Parameters.AddWithValue( @ Comments,txtComments.Text);
cmd.Parameters.AddWithValue( @ Cust_ID,txtCust_ID.Text);

txtDate.Text = Convert.ToString(Calendar1.SelectedDate);
txtDate.Text = Calendar1.SelectedDate.ToShortDateString();
DropTime.SelectedValue = Convert.ToString(DropTime);
txtValetType.Text = Convert.ToString(txtValetType);




cmd.ExecuteNonQuery();
con.Close();

}







亲切的问候

Neville



我的尝试:



试过以下示例 - 用于将日期和时间转换为字符串的行。我从日历中选择一个日期,它会填充一个包含所选日期的文本框。时间由我使用集合手动输入的Times填充,并根据客户所需时间选择。时间是从0800到19:30。时间分为半小时,例如0800 -0830-0900-0930,依此类推,最后一个规定只提供快递代客,例如19:30 Express Only。我不确定字符串Express Only是否会导致任何问题,但我现在看不到任何问题。



如果有人能查看我的代码并帮助我我遇到的错误,我将非常感激。

解决方案

您为SqlParameters提供的值必须是您为其指定的类型。如果你有一个带有SqlDbType.Date的SqlParameter,那么你需要提供一个DateTime类型的值,但你提供了一个字符串。



对于SqlDbType.Time你还需要提供DateTime类型的值。



对于其他参数,您没有指定SqlDbType,但这并不能减轻您必须提供的对象正确的类型。对于整数列,您必须提供整数值,依此类推。



您应该使用适当的.Parse(..)或.TryParse(..)方法根据类型将字符串值解析为正确类型的对象,例如:

 DateTime日期; 
if (!DateTime.TryParse(txtDate.Text, out date))
{
// 处理转换错误(例如显示错误消息)
}

// ...

cmd .Parameters.AddWithValue( @ Date,SqlDbType.Date).Value = date;

// ...



基本上对于整数和其他类型也是如此。


首先,永远不要在 varchar 中存储数值数据。为此目的使用数字SQL数据类型。



有时,您没有选择,必须将字符串数据解释为数字。在这种情况下,永远不要使用 Convert.ToString 。它可能(或可能不)起作用,但......这不是真正的转换。这是字符串解析,成功与否。更好地使用 int.Parse ,或更好的 int.TryParse (第二个没有抛出异常),以及与其他类型相同名称的方法:

Int32.GetTypeCode方法(系统) [ ^ ]。



-SA

Here is my code if anyone can help me please. I have created a booking ASP.NET Web Application 4.5 and are having problems on the admin page where the admin user can create a manual booking. Below are two errors i am having.

1: Conversion failed when converting the nvarchar value 'Full' to data type int.
2:Cannot convert char value to smallmoney. The char value has incorrect syntax.

here is my code where it is failling.

protected void Button1_Click1(object sender, EventArgs e)
    {
        // make a new job record and insert the data from the form - if there is no Cist_id it has to go in as a null

        SqlCommand cmd = new SqlCommand("INSERT INTO Job( Date, Start_Time, Valet_ID, Additions, Car_Reg, Worker_ID, Comments, Cust_ID) Values(@Date, @Start_Time, @Valet_ID, @Additions, @Car_Reg, @Worker_ID, @Comments, @Cust_ID)", con);
        cmd.Parameters.AddWithValue("@Date", SqlDbType.Date).Value = txtDate.Text;
        cmd.Parameters.AddWithValue("@Start_Time", SqlDbType.Time).Value = DropTime.Text;
        cmd.Parameters.AddWithValue("@Valet_ID", txtValetType.Text);
        cmd.Parameters.AddWithValue("@Additions", txtAdditions.Text);
        cmd.Parameters.AddWithValue("@Car_Reg", txtCarReg.Text);
        cmd.Parameters.AddWithValue("@Worker_ID", txtWorker_ID.Text);
        cmd.Parameters.AddWithValue("@Comments", txtComments.Text);
        cmd.Parameters.AddWithValue("@Cust_ID", txtCust_ID.Text);

        txtDate.Text = Convert.ToString(Calendar1.SelectedDate);
        txtDate.Text = Calendar1.SelectedDate.ToShortDateString();
        DropTime.SelectedValue = Convert.ToString(DropTime);
        txtValetType.Text = Convert.ToString(txtValetType);
       

        
       
        cmd.ExecuteNonQuery();
        con.Close();
       
    }




Kind Regards
Neville

What I have tried:

have tried examples on-line to convert the date and time to a string. I pick a date from the calendar and it populates a text box with the selected date. The time is populated by Times that I have manually entered using collection, and is selected according to the customers required time. The times are from 0800hrs to 1930hrs. The times are split avery half an hour, for example 0800 -0830-0900-0930 and so on, and the last one stipulates that a express Valet is only available, for example 19:30 Express Only. I am not sure if the string Express Only is causing any issues but I cant see any at the moment.

If anyone could look at my code and help me with the errors I am having , I would be most grateful.

解决方案

The value you provide for SqlParameters has to be of the type that you specified for it. If you have a SqlParameter with SqlDbType.Date then you need to provide a value of type DateTime, but you provided a string.

For SqlDbType.Time you also need to provide a value of type DateTime.

For the other parameters you didn't specify a SqlDbType, but that doesn't relieve you of having to provide an object of the correct type. For integer-columns you have to provide an integer value and so on.

You should use the appropriate .Parse(..) or .TryParse(..) methods of the according types to parse the string value into an object of the correct type, e.g.:

DateTime date;
if (!DateTime.TryParse(txtDate.Text, out date))
{
   // handle conversion error (e.g. show error message)
}

// ...

cmd.Parameters.AddWithValue("@Date", SqlDbType.Date).Value = date;

// ...


Basically the same goes for integers and other types.


First of all, never store numeric data in varchar. Use numeric SQL data types for this purpose.

Sometimes, you have no choice and have to interpret string data as numeric. In this case, never use Convert.ToString. It may (or may not) work, but… this is not really conversion. This is string parsing, successful or not. Better use int.Parse, or better int.TryParse (the second one not throwing an exception), as well as the methods under the same names of other types:
Int32.GetTypeCode Method (System)[^].

—SA


这篇关于我的代码中有两个错误,但无法看到我出错的地方。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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