简体   繁体   English

System.InvalidCastException:将数据发布到sql表时,指定的强制转换无效

[英]System.InvalidCastException: Specified cast is not valid when posting data to sql table

I am creating an IT inventory system and need to add all the workstation information to the Workstation table. 我正在创建一个IT库存系统,需要将所有工作站信息添加到Workstation表中。 i've switched on the debugger and going through the code I receive the error 我已经打开调试器并通过我收到错误的代码

"System.InvalidCastException: 'Specified cast is not valid.' “System.InvalidCastException:'指定的强制转换无效。'

The error comes from the line code of if ((bool)item["Wkst_Status"]) 该错误来自if ((bool)item["Wkst_Status"])的行代码

I have not tried anything as yet as i am new to C#, asp.net as a beginner. 我还没有尝试任何东西,因为我是C#的新手,asp.net作为初学者。

foreach (DataRow item in dt.Rows)
{
    int n = dataGridView1.Rows.Add();
    dataGridView1.Rows[n].Cells[0].Value = item["Emp_Name"].ToString();
    dataGridView1.Rows[n].Cells[1].Value = item["Emp_Surname"].ToString();
    dataGridView1.Rows[n].Cells[2].Value = item["Department"].ToString();
    dataGridView1.Rows[n].Cells[3].Value = item["Company"].ToString();
    dataGridView1.Rows[n].Cells[4].Value = item["Hostname"].ToString();

    if ((bool)item["Wkst_Status"])
    {
        dataGridView1.Rows[n].Cells[5].Value = "Active";
    }
    else
    {
        dataGridView1.Rows[n].Cells[5].Value = "Inactive";
    }

    dataGridView1.Rows[n].Cells[6].Value = item["Make"].ToString();
    dataGridView1.Rows[n].Cells[7].Value = item["Model"].ToString();
    dataGridView1.Rows[n].Cells[8].Value = item["SerialNumber"].ToString();
    dataGridView1.Rows[n].Cells[9].Value = tem["ProductNumber"].ToString();
    dataGridView1.Rows[n].Cells[10].Value = item["Purch_Date"].ToString();
    dataGridView1.Rows[n].Cells[11].Value = item["WExpiry_Date"].ToString();
    dataGridView1.Rows[n].Cells[12].Value = item["Memory"].ToString();
    dataGridView1.Rows[n].Cells[13].Value = item["Processor"].ToString();
    dataGridView1.Rows[n].Cells[14].Value = item["HDD"].ToString();
    dataGridView1.Rows[n].Cells[15].Value = item["OS"].ToString();
}

after entering the data in the app the data should be written to the sql table 在应用程序中输入数据后,应将数据写入sql表

As a start you can do it in a safer way: 首先,您可以以更安全的方式执行此操作:

Instead of this: 而不是这个:

if ((bool)item["Wkst_Status"])

Use this: 用这个:

var isActive = Convert.ToBoolean(item["Wkst_Status"]);

if (isActive  == true)
    {
        dataGridView1.Rows[n].Cells[5].Value = "Active";
    }
    else
    {
        dataGridView1.Rows[n].Cells[5].Value = "Inactive";
    }

You can also do null checks, but in your case that won't matter, because probably if the item["Wkst_Status"] is null, you would want the value of the cell to say "InActive" 您也可以进行空检查,但在您的情况下无关紧要,因为可能如果项[[Wkst_Status“]为空,您可能希望单元格的值说”InActive“

ty this: 这个:

if(item["Wkst_Status"] != null)
{
    dataGridView1.Rows[n].Cells[5].Value = (Convert.ToBoolean(item["Wkst_Status"]) ?"Active":"Inactive");
}

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

相关问题 Linq To数据集错误System.InvalidCastException:指定的强制转换无效 - Linq To Data set error System.InvalidCastException: Specified cast is not valid 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid System.InvalidCastException:'指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar System.InvalidCastException:指定的强制转换无效(linq查询) - System.InvalidCastException: Specified cast is not valid (linq query) System.InvalidCastException:指定的强制转换无效。 -DynamoDB查询 - System.InvalidCastException: Specified cast is not valid. - DynamoDB Query System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL C#System.InvalidCastException:指定的转换无效 - C# System.InvalidCastException: Specified Cast is not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM