what is the difference between :Add and AddWithValue

Nader

Well-known member
Joined
Oct 9, 2008
Messages
55
Programming Experience
Beginner
what is the difference between :Add and AddWithValue?
 
This is what documentation is for, so for detail, RTFM but on a basic level:

Add() lets you accurately specify everything about the parameter
AddWithValue makes sensible guesses based on the runtime type of the value you pass in

AWV is a shortcut that "gets it right most of the time" but you'll never see designer generated code use it, only lazy humans. With the designer code there is too much that needs to be precisely controlled (such as size/scale/direction/db type)
 
Whenever you want to add a parameter and specify a value then you should almost universally use AddWithValue. There's really no point to using Add in that scenario. In that case you MUST make sure that the value is the correct type, e.g. if the user enters a number into a TextBox then you must convert the string to the appropriate numeric type first.

The exception to that rule would be if you were using the same command multiple times with different parameter values. In that case you'd use Add.

If you aren't specifying the values for the parameters explicitly then you should always use Add. An example of that is when you're creating the DeleteCommand, InsertCommand, and UpdateCommand of a DataAdapter. In that case the values will come from a DataTable so you can't add them yourself.

Note that, even when using Add, it's important to make sure that the values are the correct data type. If you specify a parameter of one type and then supply a value of another you'll get an exception thrown.
 
You know I've wondered the same thing, and after reading up on it I somewhat understood, but I think the question may have come from a warning that is given (At least to me) by VS2005, it's why I looked it up, for sake of not getting the below warning I changed my code to AddWithValue.

Warning 1 'Public Function Add(parameterName As String, value As Object) As System.Data.OleDb.OleDbParameter' is obsolete: 'Add(String parameterName, Object value) has been deprecated. Use AddWithValue(String parameterName, Object value). .NET Framework Developer Center' C:\Users\kmccolgan\Documents\Visual Studio 2005\Projects\DepoLedger3.1\DepoLedger2\mod_gbl.vb 3919 13 DepoLedger3

After reading this post I'm going to go back and see how screwed up things are in my project...

Oh the fun...

Edit : don't bother with the link either, its been moved or removed
 
You know I've wondered the same thing, and after reading up on it I somewhat understood, but I think the question may have come from a warning that is given (At least to me) by VS2005, it's why I looked it up, for sake of not getting the below warning I changed my code to AddWithValue.



After reading this post I'm going to go back and see how screwed up things are in my project...

Oh the fun...

Edit : don't bother with the link either, its been moved or removed
The AddWithValue method was added in .NET 2.0. Previously the same functionality was provided by an overload of the Add method. That overload has been deprecated.
 

Latest posts

Back
Top