Update query not working

G

Guest

Hi
I have wrote a stored proc (Sproc) to update one of my sqlserver tables. This is what the query looks like

ALTER PROCEDURE dbo.UpdMea
(@NewName nvarchar(50)
@NewCost Money
@MealType varchar(50)
AS UPDATE dbo.Men
SET Meal_Type = @NewName, Meal_Cost = @NewCos
WHERE (Meal_Type = @MealType

I have a combobox that contains a list of meals. When the value is changed a sproc is fired aginst my DB that retrieve the Meal Name and Price based on what is selected in the combobox. The results of this query are then binded to 2 textboxes. The user can then update the values in the textboxes and click the Save button to propogate the changes back to the database
The problem i have is the changes do not seem to appear in my database and no message is displayed showing me a possible error. Can anyone see if i am missing anything in the onclick event code for my save button.....

If MsgBox("Are you sure you want to save your updates?", MsgBoxStyle.OKCancel Or MsgBoxStyle.Question, "Save?") = MsgBoxResult.OK The

Dim cmdUpdMeal As New SqlCommand("UpdMeal", Conn
cmdUpdMeal.CommandType = CommandType.StoredProcedur

cmdUpdMeal.Parameters.Add("@NewName", TextBox1.Text
cmdUpdMeal.Parameters.Add("@NewCost", Convert.ToDecimal(TextBox2.Text)
cmdUpdMeal.Parameters.Add("@MealType",
CBoxEditMealName.SelectedText.ToString

Tr
Conn.Open(
cmdUpdMeal.ExecuteNonQuery(
Catch x As Exceptio
MsgBox(x.Message
Finall
Conn.Close(
End Tr
End If
 
M

Miha Markic [MVP C#]

Hi,

Did you check what values are actually assigned to parameters?

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Bhavna said:
Hi,
I have wrote a stored proc (Sproc) to update one of my sqlserver tables.
This is what the query looks like:
ALTER PROCEDURE dbo.UpdMeal
(@NewName nvarchar(50),
@NewCost Money,
@MealType varchar(50))
AS UPDATE dbo.Menu
SET Meal_Type = @NewName, Meal_Cost = @NewCost
WHERE (Meal_Type = @MealType)

I have a combobox that contains a list of meals. When the value is changed
a sproc is fired aginst my DB that retrieve the Meal Name and Price based on
what is selected in the combobox. The results of this query are then binded
to 2 textboxes. The user can then update the values in the textboxes and
click the Save button to propogate the changes back to the database.
The problem i have is the changes do not seem to appear in my database and
no message is displayed showing me a possible error. Can anyone see if i am
missing anything in the onclick event code for my save button......
If MsgBox("Are you sure you want to save your updates?",
MsgBoxStyle.OKCancel Or MsgBoxStyle.Question, "Save?") = MsgBoxResult.OK
Then
 
G

Guest

Hi Miha

How would i check these parameter values?
I inserted the following line in the 'Try' Part of the code to check what the parameter contains...
MsgBox(cmdUpdMeal.Parameters("@MealType").Value
is this what u r asking
This gave an empty messagebox!!! The code i used is below
Could the fact that i have used databindings on the textbox that are being edited by the user be the case??

i.e a combobox value is changed. When it is changed a stored proc is run against my database. The results of this are then being shown in textbox1 and textbox2. The user can then update this and click a save button. The code of the save button is below...

If MsgBox("Are you sure you want to save your updates?", MsgBoxStyle.OKCancel Or MsgBoxStyle.Question, "Save?") = MsgBoxResult.OK The

Dim cmdUpdMeal As New SqlCommand("UpdMeal", Conn
cmdUpdMeal.CommandType = CommandType.StoredProcedur

cmdUpdMeal.Parameters.Add("@NewName", SqlDbType.Char, 50
cmdUpdMeal.Parameters.Add("@NewCost", SqlDbType.Money
cmdUpdMeal.Parameters.Add("@MealType", SqlDbType.Char, 50

cmdUpdMeal.Parameters("@NewName").Value = TextBox1.Tex
cmdUpdMeal.Parameters("@NewCost").Value = Convert.ToDecimal(TextBox2.Text
cmdUpdMeal.Parameters("@MealType").Value = CBoxEditMealName.SelectedText.ToStrin

Tr
Conn.Open(
cmdUpdMeal.ExecuteNonQuery(
MsgBox(cmdUpdMeal.Parameters("@MealType").Value
Catch x As Exceptio
MsgBox(x.Message
Finall
Conn.Close(
End Tr
End I

Any idea why i cant get an update?
 
M

Miha Markic [MVP C#]

Hi,

Ahh, I see now.
*CBoxEditMealName.SelectedText.ToString* it returns you the selected text
within combobox.
You should use Text property instead: CBoxEditMealName.Text.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Bhavna said:
Hi Miha,

How would i check these parameter values??
I inserted the following line in the 'Try' Part of the code to check what the parameter contains....
MsgBox(cmdUpdMeal.Parameters("@MealType").Value)
is this what u r asking?
This gave an empty messagebox!!! The code i used is below.
Could the fact that i have used databindings on the textbox that are being
edited by the user be the case???
i.e a combobox value is changed. When it is changed a stored proc is run
against my database. The results of this are then being shown in textbox1
and textbox2. The user can then update this and click a save button. The
code of the save button is below....
If MsgBox("Are you sure you want to save your updates?",
MsgBoxStyle.OKCancel Or MsgBoxStyle.Question, "Save?") = MsgBoxResult.OK
Then
 
Top