【发布时间】:2022-11-27 09:06:26
【问题描述】:
调试时,出现“int StartKmReading = Convert.ToInt32(txtDTStartKmReading);” 然后它显示如下 errpr
System.InvalidCastException:“无法将‘System.Windows.Forms.TextBox’类型的对象转换为‘System.IConvertible’类型。”
数据库的数据类型和名称也是正确的。
请帮我解决一下这个。谢谢
`
private void btnAddDT_Click(object sender, EventArgs e)
{
try
{
String InvoiceNo = txtDTInvoice.Text;
String VehicleNo = txtDTVehicleNo.Text;
String PackageType = txtDTPackageType.Text;
DateTime StartTime = dtpStartTimeDT.Value;
DateTime EndTime = dtpEndtimeDT.Value;
int StartKmReading = Convert.ToInt32(txtDTStartKmReading);
int EndKmReading = Convert.ToInt32(txtDTEndKmReading.Text);
double BaseHire = Convert.ToDouble(txtBaseHireChargeDT.Text);
double WaitingFee = Convert.ToDouble(txtWaitingFeeDT.Text);
double ExtraKmCharge = Convert.ToDouble(txtExtraKmChargeDT.Text);
double TotalAmount = Convert.ToDouble(txtDTTotalAmountCal.Text);
conn.Open();
String addQ = "insert into DayTourHires Values ('" + InvoiceNo + "', '" + VehicleNo + "', '" + PackageType + "', '" + StartTime+ "', '" + EndTime + "', '" + StartKmReading + "', '" + EndKmReading + "', '" + BaseHire + "', '" + WaitingFee + "', '" + ExtraKmCharge + "', '" + TotalAmount + "')";
SqlCommand comm = new SqlCommand(addQ, conn);
comm.ExecuteNonQuery();
MessageBox.Show("Record Inserted");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
`
数据应该成功地存储在数据库中而没有错误。
【问题讨论】:
-
Convert.ToInt32(txtDTStartKmReading);- 您需要像所有其他文本框一样从txtDTStartKmReading获取Text属性。这段代码也开放了SQL注入