2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Data / System.Data.Odbc / OdbcParameter.cs
index 407dd52689f8b64a87dfad209501d2d6591d80d7..77c2d7e083c755df0d3a086518be63844f22239b 100644 (file)
@@ -81,6 +81,17 @@ namespace System.Data.Odbc
                {
                        this.name = name;
                        this.ParamValue = value;
+                        
+                        if (value != null && !value.GetType ().IsValueType) {
+                                Type type = value.GetType ();
+                                if (type.IsArray)
+                                        size = type.GetElementType () == typeof (byte) ? 
+                                                ((Array) value).Length : 0;
+                                else
+                                        size = value.ToString ().Length;
+                        }
+
+
                }
 
                public OdbcParameter (string name, OdbcType dataType) 
@@ -254,22 +265,26 @@ namespace System.Data.Odbc
                }\r
 \r
                private void setBuffer() {\r
-                       // Load buffer with new value\r
-                       if (odbcType == OdbcType.Int)\r
-                               intbuf = (int)ParamValue;\r
-                       else {\r
+                       // Load buffer with new value
+                       if (odbcType == OdbcType.Int)
+                                intbuf = ParamValue == null ? new int () : (int) ParamValue;
+                        else {\r
                                string paramValueString = ParamValue.ToString();\r
                                // Treat everything else as a string\r
                                // Init string buffer\r
                                 if (ParamValue is String)
                                         paramValueString = "\'"+paramValueString+"\'";
+
+                                 int minSize = size;
+                                 minSize = size > 20 ? size : 20;
+                                if (buffer == null || buffer.Length < minSize)
+                                         buffer = new byte[minSize];
+                                 else
+                                         buffer.Initialize();
                                  
-                                if (buffer == null || buffer.Length < ((size > 20) ? size : 20))\r
-                                       buffer = new byte[(size > 20) ? size : 20];\r
-                               else\r
-                                       buffer.Initialize();\r
-                               // Convert value into string and store into buffer\r
-                               System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, paramValueString.Length, buffer, 0);\r
+                                 // Convert value into string and store into buffer
+                                 minSize = paramValueString.Length < minSize ? paramValueString.Length : minSize;
+                                 System.Text.Encoding.ASCII.GetBytes(paramValueString, 0, minSize, buffer, 0);\r
                        }\r
                        bufferIsSet = true;\r
                }\r