从'System.String'到'System.Byte []'的无效转换. [英] Invalid cast from 'System.String' to 'System.Byte[]'.

查看:111
本文介绍了从'System.String'到'System.Byte []'的无效转换.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
Plz 请帮助我.

Hello
Plz Please help me with this.

protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString );
            
           
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text.Trim();
cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = txtDesc.Text.Trim();
cmd.Parameters.Add("@Image", SqlDbType.Image).Value =txtImage.Text.Trim();
cmd.Parameters.Add("@Active", SqlDbType.VarChar).Value = txtActive.Text.Trim();
cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = txtCreatedBy.Text.Trim();
cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = txtCreDate.Text.Trim();

cmd.Connection = con;
cmd.CommandText = "sp_SqlImage";

//cmd=new SqlCommand ("sp_SqlImage", con);
           con.Open();
            //cmd.CommandType =CommandType.StoredProcedure;
            cmd.ExecuteReader();

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


我收到此错误 plz 请帮助


I get this error plz please help

Invalid cast from 'System.String' to 'System.Byte[]'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'System.Byte[]'.

Source Error: 


Line 78:             con.Open();
Line 79:             cmd.CommandType =CommandType.StoredProcedure;
Line 80:             cmd.ExecuteReader();
Line 81: 
Line 82:

推荐答案

您在此行有错误

you have error in this line

cmd.Parameters.Add("@Image", SqlDbType.Image).Value =txtImage.Text.Trim();



在这种情况下,您无法保存文本值.
您在此处存储此数据类型的字节数组.



in this you cannot save text value.
you store here byte array for this datatype.


检查数据库中的图像字段名称(其图像数据类型以字节格式存储数据),然后以字符串格式传递数据...这就是为什么它给你错误..

用于检查以使您的图片字段为空或删除

Check out the image field name in database(its datatype of image which stores the data in byte format) and you are passing the data in string format...thats why its giving you the error..

for checking make your image field to null or remove the

cmd.Parameters.Add("@Image", SqlDbType.Image).Value =txtImage.Text.Trim();



然后保存数据...,您将轻松识别错误..


谢谢



then save your data... you will recognize the error easily..


Thanks


这篇关于从'System.String'到'System.Byte []'的无效转换.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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