Doubt2 - Can anyone explain the difference?

Posted by Arjunan_Csharp under C# on 1/18/2013 | Points: 10 | Views : 478 | Status : [Member] | Replies : 6
1)Cmd.Parameters.Add("@ID", SqlDbType.Int, 4).Value = idTextField.Text;

2)Cmd.Parameters.Add("@ID", SqlDbType.Int, 4);
Cmd.Parameters.Add["@ID"].Value=idTextField.Text;
What is the difference between these 2 things?

I am newbie in dotnet world.please answer for stupid doubts? :)




Responses

Posted by: Kundnani.Rt on: 1/19/2013 [Member] Starter | Points: 25

Up
0
Down
First one is Correct.

Second one is C# Syntax Error

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arjunan_Csharp on: 1/20/2013 [Member] Starter | Points: 25

Up
0
Down
Hi..Can u please what the mistake is?

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kundnani.Rt on: 1/21/2013 [Member] Starter | Points: 25

Up
0
Down
Problem with second one is that the compiler evaluates it as the indexer [] , but Add() is a method .

So it is a compiler error for your statement.

However, the first one is perfectly fine.

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/22/2013 [Member] Starter | Points: 25

Up
0
Down
first one is correct go for it...

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 1/22/2013 [Member] Starter | Points: 25

Up
0
Down
hi..

The difference is.....one is method and other one is indexer....


The way we should provide the value are listed below :


1) cmd.Parameters.Add("@ID", SqlDbType.Int, 4);
2) cmd.Parameters.AddWithValue("@ID", 4);


In the first mehod, we have to specify the SQLDbType while the other method uses default datatype i.e. Object type.

The indexers are not useful for this......



Sourabh07

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 1/23/2013 [Member] Bronze | Points: 25

Up
0
Down
Cmd.Parameters.Add("@ID", SqlDbType.Int, 4);
Cmd.Parameters.AddWithValue("@ID",idTextField.Text);

Now both are correct



Thanks
Pavan Kumar
Mark Answer if this fits the need

Arjunan_Csharp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response