Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am building windows form application.i have to use funtion which is defined in another form how can i call in update query.
"update mytable set id=1 where name=?(here i want to call a funtion i-e fetchname())
Posted

1 solution

Call the function, and add the returned value as a parameter:
C#
using (SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
using (SqlCommand command = new SqlCommand("UPDATE mytable SET id = 1 WHERE name = @name", connection))
{
    command.Parameters.AddWithValue("@name", fetchname());
    
    connection.Open();
    command.ExecuteNonQuery();
}
 
Share this answer
 
Comments
CPallini 26-Sep-14 7:49am    
5.
anup.bhunia 26-Sep-14 8:02am    
note that fetchname(), if defined in another form, you cant call it, unless it is a static function.

better approach would be to bring the function to a separate class and call from both the form.
Nainaaa 27-Sep-14 14:48pm    
its not updating the field to 1.
Richard Deeming 29-Sep-14 7:24am    
Debug your code. Make sure the method is being called, and that the parameter is set to the value you are expecting.

If it still doesn't work, try running the query in SQL Management Studio. If that doesn't update the record, then there isn't a record in mytable with the specified name.

If running the query from SQL Management Studio updates the record, then your application is not looking at the same database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900