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

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

问题描述

你好

我的添加按钮代码:

Hello

My Code for add button:

protected void Button1_Click(object sender, EventArgs e)
        {
            con = new SqlConnection("server=203.199.15.181; database= commonlogin;uid=sa;pwd=nirwan612");
            cmd.Parameters.Add("@ID", SqlDbType.BigInt).Value =txtID.Text;
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
            cmd.Parameters.Add("@Description", SqlDbType.VarChar).Value = txtDesc.Text;
            cmd.Parameters.Add("@Image", SqlDbType.Binary).Value = txtImage.Text;
            cmd.Parameters.Add("@Active", SqlDbType.VarChar).Value = txtActive.Text;
            cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = txtCreatedBy.Text;
            cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = txtCreDate.Text;

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

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


----------------------------------------
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[]
---------------------------------------------



请帮助我解决此错误



Plz Help i get this error

推荐答案

将字符串转换为字节数组的最简单方法是使用实​​例化文本编码类System的GetBytes()方法. Text.ASCIIEncoding.
The easiest way to convert a string into a byte array is to use the GetBytes() method of an instantiated text encoding class System.Text.ASCIIEncoding.


saurabh,

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

在这里,您将字符串分配为二进制类型....您不能这样做...
给出其他数据类型或分配二进制值而不是字符串.
saurabh,

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

Here u r assigning a string to binary type.... u cannot do it...
Give a different datatype or assign binary value instead of string.


saurabh,

您从
得到错误 cmd.Parameters.Add("@ Image",SqlDbType.Binary).Value = txtImage.Text;

您正在尝试将文本转换为二进制,这是无效的,
您必须将文本值转换为二进制值,或者将数据类型更改为text.

选择是您的更适合您的选择.
saurabh,

you are getting the error from
cmd.Parameters.Add("@Image", SqlDbType.Binary).Value = txtImage.Text;

you are trying to convert the text to binary, that is not valid,
Either you have to Cast the text value to binary or change the datatype to text .

Choice is your''s what is preferable to you.


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

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