Reputation: 85
I am trying to update my database using VB in my .net application. I have added a data_Set file in my app_code folder and uploaded my database to it. I cant seem to update the table and insert new rows to it. (I am new to database) The database file is .mdf and I followed the instructions for data access layers (generated sql statements).
here is my web.config file:
<configuration>
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CreatePatient.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
<add name="CreatePatientConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\CreatePatient.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
</system.web>
</configuration>
Here is my VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ErrorTXT.Visible = False
End Sub
Protected Sub SignUp_Click(sender As Object, e As System.EventArgs) Handles SignUp.Click
Dim CCSQL As New SqlConnection
Dim CCUser As New SqlCommand
Dim strSQL As String
If IsValid Then
Try
CCSQL.ConnectionString = ConfigurationManager.ConnectionStrings("CreatePatientConnectionString").ConnectionString
strSQL = "insert into CreatePatient (Account, Password, FirstName, LastName, Address, HomePhone, Mobile, City, State, Zip, Social, Age, Gender, Height, Weight, Marital, Spouse) values (@Account, @Password, @FirstName, @LastName, @Address, @HomePhone, @Mobile, @City, @State, @Zip, @Social, @Age, @Gender, @Height, @Weight, @Marital, @Spouse)"
CCUser.CommandText = strSQL
CCUser.CommandType = Data.CommandType.Text
CCUser.Parameters.Add("@Account", Data.SqlDbType.VarChar).Value = Account.Text
CCUser.Parameters.Add("@Password", Data.SqlDbType.VarChar).Value = Password.Text
CCUser.Parameters.Add("@FirstName", Data.SqlDbType.VarChar).Value = FirstName.Text
CCUser.Parameters.Add("@LastName", Data.SqlDbType.VarChar).Value = LastName.Text
CCUser.Parameters.Add("@Address", Data.SqlDbType.VarChar).Value = Address.Text
CCUser.Parameters.Add("@HomePhone", Data.SqlDbType.VarChar).Value = HomePhone.Text
CCUser.Parameters.Add("@Mobile", Data.SqlDbType.VarChar).Value = Mobile.Text
CCUser.Parameters.Add("@City", Data.SqlDbType.VarChar).Value = City.Text
CCUser.Parameters.Add("@State", Data.SqlDbType.VarChar).Value = State.Text
CCUser.Parameters.Add("@Zip", Data.SqlDbType.Int).Value = ZipCode.Text
CCUser.Parameters.Add("@Social", Data.SqlDbType.VarChar).Value = SSN.Text
CCUser.Parameters.Add("@Age", Data.SqlDbType.Int).Value = CurrentAge.Text
CCUser.Parameters.Add("@Gender", Data.SqlDbType.VarChar).Value = Gender.SelectedValue
CCUser.Parameters.Add("@Height", Data.SqlDbType.VarChar).Value = Height.Text
CCUser.Parameters.Add("@Weight", Data.SqlDbType.VarChar).Value = Weight.Text
CCUser.Parameters.Add("@Marital", Data.SqlDbType.VarChar).Value = Marital.SelectedValue
CCUser.Parameters.Add("@Spouse", Data.SqlDbType.VarChar).Value = Spouse.Text
CCSQL.Open()
CCUser.Connection = CCSQL
Response.Redirect("ProcedureSelectionForm.aspx")
CCSQL.Close()
Catch ex As Exception
ErrorTXT.Text = ex.Message
End Try
End If
End Sub
End Class
The idea is to fill out the account credentials and store them in the database. Let me know if I am missing any information. I am new to stackoverflow, ASP.net, and SQL so ty for the help
Upvotes: 2
Views: 156
Reputation: 216243
Your code is missing of the call to ExecuteNonQuery that inserts the data in your database
If IsValid Then
Try
Dim strSQL = "insert into CreatePatient (Account, Password, FirstName, LastName, " & _
"Address, HomePhone, Mobile, City, State, Zip, Social, Age, Gender, " & _
"Height, Weight, Marital, Spouse) values (@Account, @Password, @FirstName, " & _
"@LastName, @Address, @HomePhone, @Mobile, @City, @State, @Zip, @Social, " & _
"@Age, @Gender, @Height, @Weight, @Marital, @Spouse)"
Using CCSQL = new SqlConnection(ConfigurationManager.ConnectionStrings("CreatePatientConnectionString").ConnectionString)
Using CCUser = new SqlCommand(strSQL, CCSQL)
CCSQL.Open()
CCUser.Parameters.Add("@Account", Data.SqlDbType.VarChar).Value = Account.Text
......
CCUser.ExecuteNonQuery()
End Using
End Using
Response.Redirect("ProcedureSelectionForm.aspx")
Catch ex As Exception
ErrorTXT.Text = ex.Message
End Try
End If
I have also enclosed the creation of the SqlConnection
and the SqlCommand
in a Using Statement that ensures the proper closing and disposing of the connection and the command also in case of Exceptions
Upvotes: 3