Reputation: 1239
I am trying to grab a querystring from the URL and send it to my stored procedure in MSSQL. The querystring is of type varbinary and when i try to send it my application is throwing an exception. I also wanted to return the select statement at the bottom of my storedprocedure that simply says Select 'Processed'
Upvotes: 0
Views: 1506
Reputation: 1239
had to actually create a function to parse hex code and then send it to the database
static byte[] ParseHexString(string value)
{
if (string.IsNullOrEmpty(value)) return null;
if (1 == (1 & value.Length)) throw new ArgumentException("Invalid length for a hex string.", "value");
int startIndex = 0;
int length = value.Length;
char[] input = value.ToCharArray();
if ('0' == input[0] && 'x' == input[1])
{
if (2 == length) return null;
startIndex = 2;
length -= 2;
}
Func<char, byte> charToWord = c =>
{
if ('0' <= c && c <= '9') return (byte)(c - '0');
if ('A' <= c && c <= 'F') return (byte)(10 + c - 'A');
if ('a' <= c && c <= 'f') return (byte)(10 + c - 'a');
throw new ArgumentException("Invalid character for a hex string.", "value");
};
byte[] result = new byte[length >> 1];
for (int index = 0, i = startIndex; index < result.Length; index++, i += 2)
{
byte w1 = charToWord(input[i]);
byte w2 = charToWord(input[i + 1]);
result[index] = (byte)((w1 << 4) + w2);
}
return result;
}
Upvotes: 2
Reputation: 100
Aghilas has specified how to assign value as byte array, and in the second line and the regular way of passing as values
Upvotes: 0
Reputation: 28990
If you wish Byte[] type, You can try with this code, you don't pass string
cmd.Parameters.Add("@dec", SqlDbType.VarBinary).Value = ;//Relpace with your new Byte[]
Or if you want string type, you can try with string type
cmd.Parameters.Add("@dec", SqlDbType.VarChar).Value = QS;//Your string
Link : http://msdn.microsoft.com/fr-fr/library/system.data.sqldbtype%28v=vs.80%29.aspx
Upvotes: 1
Reputation: 34790
Shouldn't you send a byte array (byte[]
) for a binary? I don't think it will accept pure strings. Try converting it to byte array with System.Text.Encoding.UTF8.GetBytes
method.
UPDATE: This question's answer tells to use a special type for binary data: What SqlDbType maps to varBinary(max)?
Upvotes: 0