12 Replies - 3562 Views - Last Post: 07 March 2012 - 08:18 PM Rate Topic: -----

#1 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Question why do we have to use .Parameters.Clear() ?

Posted 01 March 2012 - 04:02 AM

whats the meaning of this?
i get this from PsychoCoder

This is the link topic

sample:

cmdDelete.Parameters.Clear()
?? :helpsmilie:

This post has been edited by AdamSpeight2008: 01 March 2012 - 11:26 AM
Reason for edit:: Stop the shouty man


Is This A Good Question/Topic? 0
  • +

Replies To: Question why do we have to use .Parameters.Clear() ?

#2 nK0de  Icon User is offline

  • Catch me As Exception
  • member icon

Reputation: 205
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: Question why do we have to use .Parameters.Clear() ?

Posted 01 March 2012 - 04:06 AM

.clear() method removes all the parameter objects from the collection.
Was This Post Helpful? 0
  • +
  • -

#3 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Question why do we have to use .Parameters.Clear() ?

Posted 01 March 2012 - 06:58 AM

View PostnK0de, on 01 March 2012 - 04:06 AM, said:

.clear() method removes all the parameter objects from the collection.



can you explain further?
Was This Post Helpful? 0
  • +
  • -

#4 lordofduct  Icon User is online

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,641
  • Joined: 24-September 10

Re: Question why do we have to use .Parameters.Clear() ?

Posted 01 March 2012 - 08:12 AM

http://msdn.microsof...parameters.aspx

SqlCommand.Paremeters is a collectoin of parameters, more specifically it's an IList. An IList is like an array, just with a more abstract interface. It describes a list of items... lists can be emptied. When you want to empty a list you call the 'Clear' function of the collection:

http://msdn.microsof...list.clear.aspx

So it sounds to me that they want to clear out all the parameters stored in the parameters collection of that sql command. Maybe to ensure that the parameters from any previous calls to the database aren't accidentally sent again.
Was This Post Helpful? 1
  • +
  • -

#5 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 75
  • View blog
  • Posts: 323
  • Joined: 12-January 11

Re: Question why do we have to use .Parameters.Clear() ?

Posted 01 March 2012 - 08:12 AM

If you don't remove them from the collection and you try to use them again, it will tell you that they already belong to a collection and you can't use the parameter again (for instance, if you want to add it to another command and use it again.
Was This Post Helpful? 1
  • +
  • -

#6 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Question why do we have to use .Parameters.Clear() ?

Posted 01 March 2012 - 08:19 AM

hmmm so it's better to use this

.Parameters.Clear() ?
Was This Post Helpful? 0
  • +
  • -

#7 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 75
  • View blog
  • Posts: 323
  • Joined: 12-January 11

Re: Question why do we have to use .Parameters.Clear() ?

Posted 01 March 2012 - 09:06 AM

It's situational. If you need to reuse the parameters, then yes, otherwise, there is no need to clear them.
Was This Post Helpful? 1
  • +
  • -

#8 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Question why do we have to use .Parameters.Clear() ?

Posted 01 March 2012 - 05:28 PM

If you use it in a loop and you want to reuse your parameters by assigning them with new values . It is best to Clear them first before assigning the new values in .

If not like Psyguy mentioned , you do not need to clear them , but just remember to dipose your SqlCommand when you are done with it . =)
Was This Post Helpful? 1
  • +
  • -

#9 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Question why do we have to use .Parameters.Clear() ?

Posted 02 March 2012 - 09:01 AM

i am using add,edit,delete,update so what do you recommend?


hmmmmm... can you explain 1 by 1 this?

parameters - ???

and

clear - ???


own explanation please? :)
Was This Post Helpful? 0
  • +
  • -

#10 bry090911  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 02-February 12

Re: Question why do we have to use .Parameters.Clear() ?

Posted 03 March 2012 - 05:55 AM

anybody please.. answer my question. :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#11 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Question why do we have to use .Parameters.Clear() ?

Posted 07 March 2012 - 04:50 PM

View Postbry090911, on 02 March 2012 - 09:01 AM, said:

i am using add,edit,delete,update so what do you recommend?


hmmmmm... can you explain 1 by 1 this?

parameters - ???

and

clear - ???


own explanation please? :)



Hey bry090911 , sorry for the late reply.. erm.. what do you mean by using add , edit , delete and update?

All of the above can use SQL parameters .


Delete From table where colname = @varName ; 





well for clear() method , as already mentioned above , it only clears all the values assigned to the parameters and not the parameter it self .
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Question why do we have to use .Parameters.Clear() ?

Posted 07 March 2012 - 07:03 PM

Quote

well for clear() method , as already mentioned above , it only clears all the values assigned to the parameters and not the parameter it self .


I don't think so... Parameters is a collection of parameter objects. Parameter objects are added to the collection. Parameters.Clear removes all parameter objects (the collection is emptied).

Just like a listbox holds a collection of Items (objects). If you then clear the listbox with Listbox.Items.Clear you empty the listbox not just the value being displayed in each item.

Quote

FROM MSDN - Removes all OleDbParameter objects from the OleDbParameterCollection.

Was This Post Helpful? 0
  • +
  • -

#13 shadachi  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 141
  • Joined: 25-January 08

Re: Question why do we have to use .Parameters.Clear() ?

Posted 07 March 2012 - 08:18 PM

I see.. I might have been mistaken .

Yes it seems that it clears all the objects within the collection . So it seems.. thanks for the insight Charlie . =D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1