Unable to cast object of type 'System.Decimal' to type 'System.Array'.

数据库有一个字段AppID 类型定义为numeric(38, 0)

实体描述如下

    [Key]
    [Column("APP_ID")]
    [Required(ErrorMessage = "APP_ID不能为空")]
    [MaxLength(38, ErrorMessage = "APP_ID最多不超过38个字符")]

    public Decimal APP_ID { get; set; } = 11111;


结果报错Unable to cast object of type 'System.Decimal' to type 'System.Array'.

原因,Decimal 类型,识别为Int不能设置MaxLength属性

阅读更多

unable to cast object of type‘System.DBNull’to type‘System.String’

08-26

环境:vs2005 ,SQL2005rn数据表: sj(cusid,name,pid,audit,comment) rn (int,varchar,int,varchar,varchar)rn 客户编号,客户名称,销售id,审批,意见rn窗体:客户信息的添加审核 frmshrnrn在主管审批销售添加客户的申请管理我将2个combobox添加到datagridview----dgv_tj中。rnrn希望实现的功能:rn主管在审核销售提交的客户信息时,分别选择combobox中的审批和意见两个字段,然后点击更新按钮,实现批量更新数据。rnrn遇到的问题:rn 当sj表中的字段audit,comment不是空值时,操作没有出现错误;rn 当datagridview提取的字段audit,comment是空值时,将光标移到第四列combobox时,出现错误:rnrnunable to cast object of type‘System.DBNull’to type‘System.String’rnrn代码如下:rnrn[code=C#]rnrnpublic partial class frmsh : Formrn rn private int comboBoxColumnIndex =4; rn private int comboBoxColumnIndex1 = 5;rnrn private DataTable DT = new DataTable();rn private SqlDataAdapter SDA = new SqlDataAdapter();rnrn public frmsh()rn rn InitializeComponent();rn InitComboBoxValues();rn rn this.dgv_tj.Controls.Add(this.comboBox1);rn this.dgv_tj.Controls.Add(this.comboBox2);rn this.dgv_tj.CellEnter += new DataGridViewCellEventHandler(dgv_tj_CellEnter);rn this.dgv_tj.CellLeave += new DataGridViewCellEventHandler(dgv_tj_CellLeave);rnrn rn private void InitComboBoxValues()rn rn this.comboBox1.Items.AddRange(new String[] "通过", "驳回" );rn this.comboBox2.Items.AddRange(new String[] "符合要求", "保护对象,不允许跟踪", "特批,允许跟踪");rn this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest; rn this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;rn this.comboBox2.AutoCompleteMode = AutoCompleteMode.Suggest; rn this.comboBox2.AutoCompleteSource = AutoCompleteSource.ListItems;rn rn private void dgv_tj_CellEnter(object sender, DataGridViewCellEventArgs e)rn rn if (e.ColumnIndex == comboBoxColumnIndex)rn rn rn DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];rn Rectangle rect = this.dgv_tj.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);rn this.comboBox1.Location = rect.Location;rn this.comboBox1.Size = rect.Size;rn comfirmComboBoxValue(this.comboBox1, (String)cell.Value);rn this.comboBox1.Visible = true;rn rn else if (e.ColumnIndex ==comboBoxColumnIndex1)rn rn rn DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];rn rn Rectangle rect = this.dgv_tj.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);rn this.comboBox2.Location = rect.Location;rn this.comboBox2.Size = rect.Size;rn comfirmComboBoxValue(this.comboBox2, (string)cell.Value);rn this.comboBox2.Visible = true;rn rnrnrn rnrn private void dgv_tj_CellLeave(object sender, DataGridViewCellEventArgs e)rn rn if (e.ColumnIndex == comboBoxColumnIndex)rn rn rn DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];rn cell.Value = this.comboBox1.Text;rn this.comboBox1.Visible = false;rn rn elsern if (e.ColumnIndex == comboBoxColumnIndex1)rn rn rn DataGridViewCell cell = this.dgv_tj.Rows[e.RowIndex].Cells[e.ColumnIndex];rn cell.Value = this.comboBox2.Text;rn this.comboBox2.Visible = false;rn rnrnrn rnrn private void comfirmComboBoxValue(ComboBox com, String cellValue)rn rnrn com.SelectedIndex = -1;rn rn if (cellValue == null) [color=#FF0000] //问题出现在这里!!!???请问应该如何修改?[/color]rn rn rn com.Text = " ";rn return;rn rn rn com.Text = cellValue;rn foreach (Object item in com.Items)rn rn if ((String)item == cellValue)rn rn com.SelectedItem = item;rn rn rn rn rnrnrn private void frmsh_Load(object sender, EventArgs e)rn rn string sqlstr = "select * from sj where audit is null ";rn string con = "server=T61;database=customer;uid=sa;pwd=";rn SqlConnection conn = new SqlConnection(con);rn SqlCommand SCD = new SqlCommand(sqlstr ,conn);rn SDA.SelectCommand = SCD;rn SDA.Fill(DT);rn dgv_tj.DataSource = DT;rn dgv_tj.AllowUserToAddRows = false;rn rn //给datagridview加行号rn private void dgv_tj_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)rn rn Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,rn e.RowBounds.Location.Y,rn dgv_tj.RowHeadersWidth - 4,rn e.RowBounds.Height);rnrn TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),rn dgv_tj.RowHeadersDefaultCellStyle.Font,rn rectangle,rn dgv_tj.RowHeadersDefaultCellStyle.ForeColor,rn TextFormatFlags.VerticalCenter | TextFormatFlags.Right); rn rnrn rnrn rnrn private void btupdate_Click(object sender, EventArgs e)rn rn tryrn rn DataTable result = dgv_tj.DataSource as DataTable;rn DT = result;rn rnrn SqlDataAdapter sda = new SqlDataAdapter();rn SqlConnection con = new SqlConnection();rn con.ConnectionString = "server=T61;database=customer;uid=sa;pwd=";rnrn rn SqlCommand cmd = new SqlCommand();rn cmd.Connection = con;rn cmd.CommandText = "update sj set audit=@audit,comment=@comment where cusid=@cusid";rn cmd.Parameters.Add("@audit", SqlDbType.VarChar, 4);rn cmd.Parameters[0].SourceColumn = "audit";rn cmd.Parameters.Add("@comment", SqlDbType.VarChar,100);rn cmd.Parameters[1].SourceColumn = "comment";rn cmd.Parameters.Add("@cusid", SqlDbType.Int);rn cmd.Parameters[2].SourceColumn = "cusid";rn sda.UpdateCommand = cmd;rn sda.Update(DT);rn rn catch (System.Exception ex)rn rn MessageBox.Show(ex.ToString());rn return;rn rn MessageBox.Show("更新成功!");rnrn rnrn rnrn[/code]rnrn希望得到帮助谢谢!

没有更多推荐了,返回首页