0

I am using the default text editor available using the ajax control toolkit and I need to save an image that a user can paste into the editor, into the database. The column in the database is varbinary(MAX) but when I try to save I get the error below

Implicit conversion from data type nvarchar(max) to varbinary(max) is not allowed. Use the CONVERT function to run this query

I then later converted the content to byte like so

var subgrantdesc = Convert.ToByte(grantdescription_editor.Content);  

Then in the parameter definition I have this

var param0 = new SqlParameter();
param0.ParameterName = "@desc";
param0.SqlDbType = System.Data.SqlDbType.VarBinary;
param0.Value = subgrantdesc;
sql.Parameters.Add(param0); 

However I get the error:

System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber....

The above error points to the line of code where I am converting to Byte. I would like to know how I can save data(images and text) entered through the editor control into the SQL server database. I also would like to know if the HTML formatting will be retained.

Any help will be appreciated.

Mario S
  • 11,715
  • 24
  • 39
  • 47
MaxI
  • 763
  • 2
  • 12
  • 43

1 Answers1

0

There should be two ways to solve this problem. One: provide length of the byte var. from here

using(SqlCommand cmd = new SqlCommand("INSERT INTO mssqltable(varbinarycolumn) VALUES (@binaryValue)", conn))
{
    // Replace 8000, below, with the correct size of the field
    cmd.Parameters.Add("@binaryValue", SqlDbType.VarBinary, 8000).Value = arraytoinsert;
    cmd.ExecuteNonQuery();
}

two: use BitConverter

string data = "0x" + BitConverter.ToString(subgrantdesc).Replace("-", "");
Community
  • 1
  • 1
user194076
  • 8,787
  • 23
  • 94
  • 154