Thursday, February 16, 2012

Adding data to database

would like to enquiry about the following codes to adding data to the database, it does not work.

Dim sqlConn As New SqlClient.SqlConnection
Dim sqlCmd1 As New SqlClient.SqlCommand
Dim sqlCmd2 As New SqlClient.SqlCommand
Dim insertCmd As New SqlClient.SqlCommand
Dim sdrData As SqlClient.SqlDataReader

sqlConn.ConnectionString = "server=localhost;database=Tsign;uid=sa;pwd=;"
sqlConn.Open()
sqlCmd1.CommandText = "select * from Customer where userId='" + userId.Text + "'"
sqlCmd1.Connection = sqlConn
sqlCmd2.CommandText = "select * from Customer where email='" + email.Text + "'"
sqlCmd2.Connection = sqlConn
sdrData = sqlCmd1.ExecuteReader()
If sdrData.Read() Then
userNameTaken.Visible = True
Else
sdrData = sqlCmd2.ExecuteReader()
If sdrData.Read() Then
userEmailTaken.Visible = True
Else
insertCmd.CommandText = "INSERT INTO Customer(name, address, gender, password, occupation, income, m_status, email, hPhone, oPhone, dob, tPhone, userId) VALUES('" + userId.Text + "', '" + password.Text + "', '" + email.Text + "', '" + nameCust.Text + "', '" + dob.Text + "', '" + gender.SelectedValue + "', '" + address.Text + "', '" + occupation.Text + "', '" + income.SelectedValue + "', '" + m_status.SelectedValue + "', " + hPhone.Text + ", " + oPhone.Text + ", " + tPhone.Text + ")"
insertCmd.Connection = sqlConn
sdrData = insertCmd.ExecuteNonQuery()
Dim name As String = sdrData("name")
Session("userid") = name
Response.Redirect("welcome.aspx")
End If
End If
sqlConn.Close()1. do you get any errors?
2. do you know that the 'insert' condition is being fired?|||One thought (the best one can do without any information aboutexactly what the problem is) is that you should surround [password] and [name] in square brackets, as they are reserved words.

Also, are hPhone and oPhone strings? If so, the insert statement needs single quotes around those values.|||douglas, that's more perceptive than me, and I wish I'd spotted it. then again, it's friday. have one on me.|||when i tried to browse the page in Visual studio, it popups a window with a warning, but i can preview the page anyway. and when i fill in the details and click submit, all the page does is refresh itself.

No comments:

Post a Comment