Coding error to run a stored procedure.

A

Angelina

ok im almost near completing this........thx u all for
helping me out and being patient with me :blush:)

I have written this code for an onclick event of a button
so that i can run a stored procedure i created in access.
The SPROC performs an insert of 2 values into a table.
hereis my code:

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim Cn As New SqlConnection("initial
catalog=TestCaraDBmsde;integrated security=SSPI;persist
security info=False;workstation id=USER;packet size=4096")
Dim cmd As New SqlCommand("SPInsMeal", Cn)
cmd.CommandType = CommandType.StoredProcedure

cmd.Parameters.Add( _
New SqlParameter("@Meal_desc",
SqlDbType.Char, 50)).Value = txtMealdesc.Text
cmd.Parameters.Add(New SqlParameter("@Cost",
SqlDbType.Money))

Try
Cn.Open()
cmd.ExecuteNonQuery()
Finally
Cn.Close()
End Try


I keep getting errors when i try and run this. It seems
to stop at the cmd.ExecuteNonQuery() line.

What am i doin wrong?
any help is appreciated. :blush:)
 
W

William \(Bill\) Vaughn

You've made a lot of progress, but where is the Catch of your Try/Catch
block? Dump the exception (ex.tostring) to see what's really failing.


--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
A

Angelina

I keep getting a syntax error when i tried this:

Try
Cn.Open()
cmd.ExecuteNonQuery()
Catch
ex.tostring() ''syntax error on this line
'' the ex is underlined
Finally
Cn.Close()
End Try

this catch statment will tell me what the error is?
 
W

William \(Bill\) Vaughn

Try this:
Try
Cn.Open()
cmd.ExecuteNonQuery()
Catch ex as Exception
ex.tostring() ''syntax error on this line
'' the ex is underlined
Finally
Cn.Close()
End Try

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
H

Hussein Abuthuraya[MSFT]

Without knowing what is the error message or seeing the stored procedure script, I think your problem is that you don't supply a value for the second parameter.


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
A

Angelina

I have made the amendment u suggested and the error has
vanised :blush:)

But when i check the databasefor the inserted values they
are not there. Is there something that im not doing
correctly or missing out?
 
W

William \(Bill\) Vaughn

I expect that if you don't break at the ex.ToString and dump it somewhere,
the exceptions are simply tossed.
Add some code to show the exception.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top