Reputation: 93
I have this method:
public static int ExecuteNonQuery(ref DbCommand command, out string ErrorMessage)
{
ErrorMessage = String.Empty;
DbTransaction tran = null;
DbConnection conn = null;
int result = 0;
This method is used when accessed on another page. I pass a command in the form of SQLcommand to which I pass a query, but I don't get what this out string
is for. This is someone else's code, but I want to use it for abstraction purposes of insertion queries. How should I get the errormessage parameter as it says it's an out string.
I want to do like this on one of my pages:
string Email = "[email protected]";
string Password = "Hello";
SqlCommand command = new SqlCommand("insert into J_example_Tbl(J_example_Email,J_example_Password) VALUES('"+Email+"','"+Password+"')");
PrimaryDBClass.ExecuteNonQuery(command, "Should I do like this");
Upvotes: 2
Views: 20219
Reputation: 1797
Regarding
but I don't get what this out string is for
Out is stands for pointing to a variable. The out
keyword describes parameters whose actual variable locations are copied onto the stack of the called method, where those same locations can be rewritten. This means that the calling method will access to the changed parameter.
For example: If you declare a variable named test
in class1
and want to change the value from another class, class2
, and still want to get the changed value from class1
you have to send the test variable with the out
keyword from class1
to class2
.
Which means in your method:
public static int ExecuteNonQuery(ref DbCommand command, out string ErrorMessage)
If any change occurred in variable ErrorMessage it will point to the actual variable where from it comes. So you will get the changed value from outside of the method ExecuteNonQuery().
Upvotes: 7
Reputation: 5151
The out
parameter can be used like this:
string Email = "[email protected]";
string Password = "Hello";
SqlCommand command = new SqlCommand("insert into J_example_Tbl(J_example_Email,J_example_Password) VALUES('"+Email+"','"+Password+"')");
string error;
PrimaryDBClass.ExecuteNonQuery(command, out error);
if(error != null)
....
You should probably check for the result in some other way (like checking the return value or something). Look at Jon Skeet's suggestions as well.
Upvotes: 1