I am using Visual Studio 2008 and have connected a database correctly as I have done a login that works fine, although when I try to insert information submitted in the text boxes a different table, it doesn't enter after I end program to check it still has no data in. Any ideas?
Dim con As SqlCeConnection = New SqlCeConnection("Data Source=NESdb.sdf")
Dim myDA As SqlCeDataAdapter
Dim myDataSet As DataSet
Dim dt As New DataTable()
'Connect to database'
con.Open()
'Attempt to retrieve data'
Try ' Select username and password that match'
Dim cmd As SqlCeDataAdapter = New SqlCeDataAdapter("INSERT INTO ScrapVehicles(Fname, Lname, Add1, Add2, Town, PostCode, Telephone, Mob, Email, VehicleType, RegNo, Year, Make, Model, V5, Collected, CollectionDate)" + "VALUES('" & txtFname.Text & "', '" & txtLname.Text & "', '" & txtAdd1.Text & "', '" & txtAdd2.Text & "', '" & txtTown.Text & "', '" & txtPostCode.Text & "', '" & txtTelephone.Text & "', '" & txtMob.Text & "', '" & txtEmail.Text & "', '" & comboVehicleType.Text & "', '" & txtReg.Text & "', '" & comboYear.Text & "', '" & comboMake.Text & "', '" & txtModel.Text & "', '" & chkV5.Text & "', '" & chkCollected.Text & "', '" & dtpWhen.Text & "')", con)
'Catch errors'
Catch ex As Exception
End Try
'Close connection to database'
If con.State <> ConnectionState.Closed Then
con.Close()
End If
You're building up the cmd object, but you don't execute it.
Suggest ditch the Adapter when inserting. Try this instead:
/*snipped values for brevity.*/
Dim insertSql As String = "INSERT INTO ScrapVehicles(Fname, Lname, Add1, Add2, Town) VALUES(#FName, #LName, #Add1, #Add2, #Town)"
conn.Open()
Dim cmd As New SqlCeCommand(insertSql, conn)
cmd.Parameters.Add(New SqlCeParameter("#FName", txtFirstName.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("#LName", txtLastName.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("#Add1", txtAdd1.Text.Trim()))
cmd.Parameters.Add(New SqlCeParameter("#Add2", txtAdd2.Text.Trim()))
cmd.ExecuteNonQuery()
conn.Close()
You are missing a bracket at the end
& dtpWhen.Text & "'", con)
should be
& dtpWhen.Text & "')", con)
Instead of using a SqlCeDataAdapter, use a SqlCeCommand object. And after creating it, actually use it (call ExecuteNonQuery on it). And remove the Try, Catch Ex as Exception and End Try lines so that, if an error occurs, you'll actually see it.
That's what I can see from 30 seconds of looking.
Edit
You should also look at using parameters rather than concatenating the INSERT statement together.
Related
I have tried this code but the date format keeps staying the same when the record is entered
mysqlconn = New MySqlConnection
mysqlconn.ConnectionString =
Dim reader As MySqlDataReader
Try
mysqlconn.Open()
Dim query As String
query = "insert into jadco_test.adrv (id,type_adrv,date_discov,date_notif,pssd,psed,comment,hearing_date,sanction`enter code here`_sd,sanction_ed) values('" & Tbox_ID.Text & "','" & ComboBox_adrv.SelectedItem & "', '" & DateTimePicker_DD = DateTime.Now.ToString("yyyy-MM-dd") & "','" & DateTimePicker_DN.CustomFormat & "','" & DateTimePicker_pssd.CustomFormat & "','" & DateTimePicker_psed.CustomFormat & "', '" & TextBox_comment.Text & "','" & DateTimePicker_HD.CustomFormat & "','" & DateTimePicker_SSD.CustomFormat & "','" & DateTimePicker_SED.CustomFormat & "' )"
command = New MySqlCommand(query, mysqlconn)
reader = command.ExecuteReader
MessageBox.Show("record saved")
mysqlconn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
mysqlconn.Dispose()
End Try
I have declared sql, cmd and cnt on public class, and also i have declare my database on Module.
Dim sql As String
Dim cmd As OleDb.OleDbCommand
Dim cnt As Long
Try
sql = "UPDATE [LDF] SET [Group_Code] = '" & ComboBox1.Text & "',
[Loan_Disb] ='" & TextBox1.Text & "',
[No_Of_Instalment] = '" & ComboBox2.Text & "',
[Single_Instalment]='" & TextBox2.Text & "',
[Loan_Security]='" & TextBox3.Text & "',
[Total_Amnt_TR]='" & TextBox4.Text & "',
[Member_Name]='" & TextBox5.Text & "',
[Group_Name]='" & TextBox6.Text & "',
[Date_Of_Disb]='" & DateTimePicker1.Value & "'
WHERE [Member_Number] = ' & ComboBox3.Text & '"
cmd = New OleDb.OleDbCommand(sql, Con)
cnt = cmd.ExecuteNonQuery
MsgBox("RECORD HAVE SUCCESSFUL UPDATED")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Dim cmd As New OleDbCommand
OpenConnection()
cmd.Connection = con
cmd.CommandText = "INSERT INTO [StudentInfo] ([Stud_Num], [Stud_FName], [Stud_LName], [Stud_ContactNum], [Stud_Email], [Stud_Address], [Stud_Username], [Stud_Password], [Stud_ConfirmPassword]) VALUES (" & _
txtStudNum.Text & ", '" & txtFname.Text & "', '" & txtLname.Text & "', '" & _
txtNum.Text & "', '" & txtEmail.Text & "', '" & txtAddress.Text & "' , '" & txtUsername.Text & "', '" & txtPassword.Text & "', '" & txtConPassword.Text & "')"
cmd.ExecuteNonQuery()
The error was the cmd.ExecuteNonQuery() I'm still a student and a noob in using vb.net so please could you tell me what I did wrong ?
I'm suppose to enter datatime to the database by passing this query
Dim regDate As DateTime = DateTime.Now
Dim strDate As String = regDate.ToString("yyyyMMddHHmmss")
I pass the "strDate" to the query,data type of my database table is datetime
objcon.DoExecute("INSERT INTO DistributorF VALUES('" & txtDisId.Text & "',
'" & txtDisNm.Text & "','" & txtDisAdd.Text & "',
'" & txtDisTele.Text & "','" & txtDisEmail.Text & "','" & regDate & "')")"
but it's getting error saying that
conversion failed when converting date and/or time from character string.
Help me to solve this problem
Dim regDate As DateTime = DateTime.Now
Dim strDate As String = regDate.ToString("yyyy-MM-dd HH:mm:ss")
objcon.DoExecute("INSERT INTO DistributorF VALUES('" & txtDisId.Text & "',
'" & txtDisNm.Text & "','" & txtDisAdd.Text & "',
'" & txtDisTele.Text & "','" & txtDisEmail.Text & "','" & strDate & "')")"
you have to pass strDate in query.Always use paremeterized query to avoid SQL injection
objcon.DoExecute("INSERT INTO DistributorF VALUES('" & txtDisId.Text & "',
'" & txtDisNm.Text & "','" & txtDisAdd.Text & "',
'" & txtDisTele.Text & "','" & txtDisEmail.Text & "','" & strDate & "')")"
May this works for you, at least it works for me on an acces db
Try
Dim cn As New OleDbConnection("your conection string here")
If cn.State = ConnectionState.Open Then
cn.Close()
End If
cn.Open()
Dim sSQL As String = "insert into UserInfo(Date) values(#d1)"
Dim cmd As OleDbCommand = New OleDbCommand(sSQL, cn)
Dim date As OleDbParameter = New OleDbParameter("#d1", OleDbType.Date, 15)
date.Value = DateTimePicker1.Text.ToString()
cmd.Parameters.Add(date)
If cmd.ExecuteNonQuery() Then
cn.Close()
MsgBox("successfully... ", MsgBoxStyle.Information, "Record Saved")
Else
MsgBox("failed... ", MsgBoxStyle.Critical, "Registration failed")
Return
End If
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Data Error")
Exit Sub
End Try
i hope this helps you,
regards Tom
i have some trobles in updating my database. ihave this codes :
Dim cmd As OleDbCommand
Dim sql As String
sql = "UPDATE nmat SET nip = '" & lblNipDosen.Text & "', nim = '" & TxtNIM.Text & "', ntugas = '" & TxtNtugas.Text & "', nabsensi = '" & TxtNabsen.Text & "', nuts = '" & TxtNuts.Text & "', nuas = '" & TxtNuas.Text & "' WHERE nim='" & TxtNIM.Text & "'"
conn.Open()
Dim reader As OleDbDataReader
Try
cmd = New OleDbCommand(sql, conn)
cmd.ExecuteNonQuery()
DataGridView1.Refresh()
Finally
'reader.Close()
End Try
conn.Close()
my problem is, by these code, the grid can be refreshed after i close and open this form again. but if i check in my ms Access, the data was not chanded at all. how to update my database and showed in my datagrid??
Try This :
Dim cmd As OleDbCommand
Dim rstTable As New DataTable()
Dim sql As String
sql = "UPDATE nmat SET nip = '" & lblNipDosen.Text & "', nim = '" & TxtNIM.Text & "', ntugas = '" & TxtNtugas.Text & "', nabsensi = '" & TxtNabsen.Text & "', nuts = '" & TxtNuts.Text & "', nuas = '" & TxtNuas.Text & "' WHERE nim='" & TxtNIM.Text & "'"
conn.Open()
Dim reader As OleDbDataReader
Try
cmd = New OleDbCommand(sql, conn)
rstTable.Load(cmd.ExecuteReader())
DataGridView1.DataSource =rstTable
Finally
'reader.Close()
End Try
conn.Close()
your gridview columns should have bound to a field