Click here to Skip to main content
15,892,927 members
Home / Discussions / C#
   

C#

 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 1:55
<<Tash18>>6-Jan-10 1:55 
GeneralRe: Problem with insert query Pin
SeMartens6-Jan-10 2:00
SeMartens6-Jan-10 2:00 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 2:18
<<Tash18>>6-Jan-10 2:18 
GeneralRe: Problem with insert query Pin
SeMartens6-Jan-10 2:26
SeMartens6-Jan-10 2:26 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 2:30
<<Tash18>>6-Jan-10 2:30 
GeneralRe: Problem with insert query Pin
SeMartens6-Jan-10 2:40
SeMartens6-Jan-10 2:40 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 20:35
<<Tash18>>6-Jan-10 20:35 
GeneralRe: Problem with insert query Pin
dojohansen7-Jan-10 5:31
dojohansen7-Jan-10 5:31 
It is indeed, but it's not too hard to fix. Make yourself a little helper method to add the parameters and make use of it. For example like this:

public static class Util
{
        public static void AddParameters(SqlCommand cmd, params object[] args)
        { 
            for (int i = 0; i < args.Length; i += 2)
            {
                cmd.Parameters.Add(GetParameter((string)args[i], args[i + 1]));
            }
        }

        public SqlParameter GetParameter(string name, object value)
        {
            SqlParameter p = new SqlParameter();
            p.ParameterName = name;
            p.SqlDbType = GetDbType(value);
            p.Direction = ParameterDirection.Input;
            p.Value = value;
            return p;
        }

        public SqlDbType GetDbType(object value)
        {
            if (value == null) throw new ArgumentNullException();

            if (t == typeof(int)) return SqlDbType.Int;
            if (t == typeof(string)) return SqlDbType.NVarChar;
            if (t == typeof(DateTime)) return SqlDbType.DateTime;
            if (t == typeof(Boolean)) return SqlDbType.Bit;
            if (t == typeof(byte[])) return SqlDbType.Image;
            if (t == typeof(SqlBinary)) return SqlDbType.Binary;
            if (t == typeof(Guid)) return SqlDbType.UniqueIdentifier;
            if (t == typeof(Int64)) return SqlDbType.BigInt;
            // ...

            throw new NotSupportedException("Missing support for type " + t.FullName + ".");
        }
}


A little bit of code, but you can now easily use it many places, for example like this:

SqlCommand getInsertCommand(string col1, int col2)
{
    SqlCommand cmd = new SqlCommand("INSERT [Table] VALUES (@col1, @col2);");
    Util.AddParameters(cmd, 
       "@col1", col1, 
       "@col2", col2);
    return cmd;
}

AnswerRe: Problem with insert query Pin
OriginalGriff6-Jan-10 0:37
mveOriginalGriff6-Jan-10 0:37 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 0:40
<<Tash18>>6-Jan-10 0:40 
GeneralRe: Problem with insert query Pin
OriginalGriff6-Jan-10 1:01
mveOriginalGriff6-Jan-10 1:01 
GeneralRe: Problem with insert query Pin
Ashfield6-Jan-10 1:16
Ashfield6-Jan-10 1:16 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 1:17
<<Tash18>>6-Jan-10 1:17 
GeneralRe: Problem with insert query Pin
OriginalGriff6-Jan-10 1:31
mveOriginalGriff6-Jan-10 1:31 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 1:42
<<Tash18>>6-Jan-10 1:42 
GeneralRe: Problem with insert query Pin
OriginalGriff6-Jan-10 2:07
mveOriginalGriff6-Jan-10 2:07 
GeneralRe: Problem with insert query Pin
<<Tash18>>6-Jan-10 20:37
<<Tash18>>6-Jan-10 20:37 
Questionevent firing in combo box Pin
Mahesh_Blr6-Jan-10 0:01
Mahesh_Blr6-Jan-10 0:01 
AnswerRe: event firing in combo box Pin
Mycroft Holmes6-Jan-10 0:13
professionalMycroft Holmes6-Jan-10 0:13 
Questiondatagrid row color Pin
Erdinc275-Jan-10 23:58
Erdinc275-Jan-10 23:58 
AnswerRe: datagrid row color Pin
Mycroft Holmes6-Jan-10 0:06
professionalMycroft Holmes6-Jan-10 0:06 
AnswerRe: datagrid row color Pin
Qendro6-Jan-10 0:57
Qendro6-Jan-10 0:57 
GeneralRe: datagrid row color Pin
David Skelly6-Jan-10 1:37
David Skelly6-Jan-10 1:37 
GeneralRe: datagrid row color Pin
Qendro6-Jan-10 2:37
Qendro6-Jan-10 2:37 
GeneralRe: datagrid row color Pin
David Skelly6-Jan-10 5:09
David Skelly6-Jan-10 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.