Friday, March 23, 2012

Problem with adding record into database

When I try to build my solution, it tells me that "SQLCommand" is not defined.

What's the problem?

Sub submitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'If Page.IsValid Then
Try
Dim MySQL = "addCustomerSQL"
Dim cmd As New SQLCommand("addCustomerSQL", dbConn)
cmd.Commandtype() = CommandType.StoredProcedure
cmd.Parameters().Item("@.username").Value = userText.Text
cmd.Parameters.Item("@.password").Value = passText.Text
dbConn.Open()
cmd.ExecuteNonQuery()
dbConn.Close()

Catch ex As Exception

End Try
End SubMake sure you're importing System.Data.SqlClient in your class|||Where would I put that?|||At the top of your page type this

Imports System.Data.SqlClient

Public Class......|||To follow up, I was able to get the code to compile, but nothing is being transferred to the database table after clicking submit.


Private Sub submitButton_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

'If Page.IsValid

Try

Dim MySQL = "addCustomerSQL"
Dim dbConn As New System.Data.SqlClient.SqlConnection
Dim cmd As New SqlCommand("addCustomerSQL", dbConn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@.username", KuserTextLI.Text))
cmd.Parameters.Add(New SqlParameter("@.password", KpassTextLI.Text))
cmd.Parameters.Add(New SqlParameter("@.fullname", KfnameTextLI.Text))
cmd.Parameters.Add(New SqlParameter("@.email_address", KemailTextLI.Text))
cmd.Parameters.Add(New SqlParameter("@.phone_nbr", KphoneTextLI.Text))
cmd.Parameters.Add(New SqlParameter("@.mailing_address", KaddressTextLI.Text))
cmd.Parameters.Add(New SqlParameter("@.credit_card_name", KccNameTextLI.Text))
cmd.Parameters.Add(New SqlParameter("@.credit_card_nbr", KccNumTextLI.Text))
cmd.Parameters.Add(New SqlParameter("@.credit_card_expiry_date", KccExpTextLI.Text))
cmd.Parameters.Item("@.username").Value = KuserTextLI.Text
cmd.Parameters.Item("@.password").Value = KpassTextLI.Text
cmd.Parameters.Item("@.fullname").Value = KfnameTextLI.Text
cmd.Parameters.Item("@.email_address").Value = KemailTextLI.Text
cmd.Parameters.Item("@.phone_nbr").Value = KphoneTextLI.Text
cmd.Parameters.Item("@.mailing_address").Value = KaddressTextLI.Text
cmd.Parameters.Item("@.credit_card_name").Value = KccNameTextLI.Text
cmd.Parameters.Item("@.credit_card_nbr").Value = KccNumTextLI.Text
cmd.Parameters.Item("@.credit_card_expiry_date").Value = KccExpTextLI.Text
dbConn.Open()
cmd.ExecuteNonQuery()
dbConn.Close()
Response.Redirect("http://localhost/KevinLiu/thankyou.aspx")

Catch ex As Exception

End Try
End Sub

Is there anything I need to put under the Page_Load procedure? Or how about on my html page, do I have to call the event procedure from there?

The Database tables were created first in a .sql file, so is it necessary to include the attributes in my Parameters.Add lines?|||Hi,
im not sure... but i think that the main problem would be the parameter name must be the same as the input variable name in your stored procedure.

anyway your codes are somewhat redundant. The following 2 lines are doing the same thing.

cmd.Parameters.Add(New SqlParameter("@.username", KuserTextLI.Text))
cmd.Parameters.Item("@.username").Value = KuserTextLI.Text

this would be enought if you are adding a sql parameter of type VarChar in:

cmd.Parameters.add(new sqlparameter("@.username", kusertextLi.text))

and this would give you a sql parameter of type Numeric :

cmd.Parameters.add(new sqlparameter("@.age", cint(kagetextLi.text))

note that if your stored procedure has output variables you have to specify the direction of the parameters. Example:

dim param as new sqlparameter("@.username", kusertextLi.text))
param.direction = parameterdirection.input

No comments:

Post a Comment