Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 insert value to sql server

Author  Topic 

yvette
Yak Posting Veteran

74 Posts

Posted - 2010-10-22 : 02:30:20
Hi, i'm using vb and asp.net to develop a system.
i want pass the value to database when register.
How if i want to pass two table without using stored procedure??

i try this, but gt error...

Dim Con As SqlConnection
Dim cmd As SqlCommand

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
Con = New SqlConnection("Data Source=YVETTE-PC\SQLEXPRESS08;Initial Catalog=CYCWS;Persist Security Info=True;User ID=sa;Password=p@ssword")
Con.Open()

cmd = New SqlCommand("INSERT INTO EMPLOYEE VALUES ('" + ddlPosition.SelectedItem.Value + "', '" + ddlStatus.SelectedItem.Value + "', '" + txtName.Text + "', '" + txtIdentity.Text + "', '" + txtHomeTel.Text + "', '" + txtMobileNo.Text + "', '" + txtLicenseNo.Text + "', '" + txtLicenseExpiry.Text + "', '" + txtDOB.Text + "', '" + ddlGender.SelectedItem.Value + "', '" + ddlRace.SelectedItem.Value + "', '" + ddlReligion.SelectedItem.Value + "', '" + ddlMaritalStatus.SelectedItem.Value + "', '" + ddlNationality.SelectedItem.Value + "', '" + txtAddress.Text + "', '" + txtPostalCode.Text + "', '" + txtCity.Text + "', '" + ddlState.SelectedItem.Value + "'), Con")
cmd.ExecuteNonQuery()
lblStatus.Text = "Record Saved"
ddlPosition.SelectedItem.Value = ""
ddlStatus.SelectedItem.Value = ""
txtName.Text = ""
txtIdentity.Text = ""
txtHomeTel.Text = ""
txtMobileNo.Text = ""
txtLicenseNo.Text = ""
txtLicenseExpiry.Text = ""
txtDOB.Text = ""
ddlGender.SelectedItem.Value = ""
ddlRace.SelectedItem.Value = ""
ddlReligion.SelectedItem.Value = ""
ddlMaritalStatus.SelectedItem.Value = ""
ddlNationality.SelectedItem.Value = ""
txtAddress.Text = ""
txtPostalCode.Text = ""
txtCity.Text = ""
ddlState.SelectedItem.Value = ""
Con.Close()

Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub

thanks for help...

yvette
Yak Posting Veteran

74 Posts

Posted - 2010-10-22 : 02:32:52
quote:
Originally posted by yvette

Hi, i'm using vb and asp.net to develop a system.
i want pass the value to database when register.
How if i want to pass two table without using stored procedure??

i try this, but gt error...

Dim Con As SqlConnection
Dim cmd As SqlCommand

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
Con = New SqlConnection("Data Source=YVETTE-PC\SQLEXPRESS08;Initial Catalog=CYCWS;Persist Security Info=True;User ID=sa;Password=p@ssword")
Con.Open()

cmd = New SqlCommand("INSERT INTO EMPLOYEE VALUES ('" + ddlPosition.SelectedItem.Value + "', '" + ddlStatus.SelectedItem.Value + "', '" + txtName.Text + "', '" + txtIdentity.Text + "', '" + txtHomeTel.Text + "', '" + txtMobileNo.Text + "', '" + txtLicenseNo.Text + "', '" + txtLicenseExpiry.Text + "', '" + txtDOB.Text + "', '" + ddlGender.SelectedItem.Value + "', '" + ddlRace.SelectedItem.Value + "', '" + ddlReligion.SelectedItem.Value + "', '" + ddlMaritalStatus.SelectedItem.Value + "', '" + ddlNationality.SelectedItem.Value + "', '" + txtAddress.Text + "', '" + txtPostalCode.Text + "', '" + txtCity.Text + "', '" + ddlState.SelectedItem.Value + "'), Con")
cmd.ExecuteNonQuery()
lblStatus.Text = "Record Saved"
ddlPosition.SelectedItem.Value = ""
ddlStatus.SelectedItem.Value = ""
txtName.Text = ""
txtIdentity.Text = ""
txtHomeTel.Text = ""
txtMobileNo.Text = ""
txtLicenseNo.Text = ""
txtLicenseExpiry.Text = ""
txtDOB.Text = ""
ddlGender.SelectedItem.Value = ""
ddlRace.SelectedItem.Value = ""
ddlReligion.SelectedItem.Value = ""
ddlMaritalStatus.SelectedItem.Value = ""
ddlNationality.SelectedItem.Value = ""
txtAddress.Text = ""
txtPostalCode.Text = ""
txtCity.Text = ""
ddlState.SelectedItem.Value = ""
Con.Close()

Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub

thanks for help...


in the code below, i just pass to one table.
i want pass to another table name PROFILE.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-10-22 : 02:33:44
"How if i want to pass two table without using stored procedure??"

Please explain the problem you want to solve. Do you mean you want to insert the values into both EMPLOYEE table and also another table?

NOTE: Your SQL is open to SQL Injection. Please read up on that if you are not familiar with it, otherwise people will be able to hack into your database and steal your data / drop your databases / all sorts
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-10-22 : 02:34:44
"i want pass to another table name PROFILE"

Sorry, the part I don't understand is "PASS" ... what do you want to do with the PROFILE table ?
Go to Top of Page

yvette
Yak Posting Veteran

74 Posts

Posted - 2010-10-22 : 02:43:53
my problem:
1. i want to pass txtUserName and txtPassword to table name PROFILE and pass all the above data to table EMPLOYEE.
(In the example before, i just write INSERT INTO EMPLOYEE)
How i want to add the txtUserName and txtPAssword to PROFILE table at the same time.

2. i want display a message like "record saved" if successful insert otherwise will get error message. Meantime display "Record Saved", the record enter will be empty at register page.

3. i want display a error message say that UserName exist, so that no same Username.

thanks

thanks
Go to Top of Page
   

- Advertisement -