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'
-
What exception does it throw?Abe Miessler– Abe Miessler2012-09-10 18:26:48 +00:00Commented Sep 10, 2012 at 18:26
-
+ ServerVersion 'con.ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}Tim– Tim2012-09-10 19:22:43 +00:00Commented Sep 10, 2012 at 19:22
4 Answers
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;
}
Comments
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
2 Comments
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)?
4 Comments
byte[] buf = System.Text.Encoding.UTF8Encoding.GetBytes(yourString); cmd.Parameters.Add("@dec", SqlDbType.VarBinary).Value = buf;