Click here to Skip to main content
15,900,482 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to insert a record to sql server 2005, but the record wasn't save into database. What's the problem?

Here my code:
Imports System.Data
Imports System.Data.SqlClient
Public Class Coba
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim reader As SqlDataReader
    Dim constring As String
    Dim SqlString As String
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim ra As Integer
    Private Sub Coba_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the '_FKIP_UNILAKDataSet.Ruangan' table. You can move, or remove it, as needed.
        Me.RuanganTableAdapter.Fill(Me._FKIP_UNILAKDataSet.Ruangan)
        constring = "Data Source=RAGAZ-PC\SQLEXPRESS;Initial Catalog=FKIP-UNILAK;Integrated Security=True"
        con = New SqlConnection(constring)
        con.Open()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New SqlClient.SqlConnection

        Dim strCon As String = "Data Source=RAGAZ-PC\SQLEXPRESS;Initial Catalog=FKIP-UNILAK;Integrated Security=True"

        Dim strCommand As String = ("Insert into dbo.Ruangan (Id_Ruangan,Nama_Ruangan,Jenis_Ruangan) values (‘" & Me.id.Text & "’,‘" & Me.nama.Text & "’,‘" & Me.jenis.Text & "’)")

        Try

            con.ConnectionString = strCon

            Dim cm As New SqlClient.SqlCommand(strCommand, con)

            con.Open()

            cm.ExecuteNonQuery()

            MsgBox("Succesfully saved…")

        Catch ex As Exception

            MessageBox.Show(ex.Message, "Error")

        Finally

            If con.State = ConnectionState.Open Then

                con.Close()

            End If

        End Try
        
    End Sub
End Class
Posted
Updated 20-Feb-11 17:49pm
v2
Comments
Abhinav S 20-Feb-11 23:32pm    
What error do you get?
Abdi tombang 20-Feb-11 23:49pm    
I'm using
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")

and it give me a message :
Incorrect syntax near '".

Where is an incorrect syntax in my code??

Hi,
Abdi

Please provide DataType of Id_Ruangan if it was Number then you should remove double quote from Me.id.Text


Thanks,
Imdadhusen
 
Share this answer
 
Comments
Albin Abel 21-Feb-11 0:00am    
yes, that is one of the issue,
Sunasara Imdadhusen 21-Feb-11 0:27am    
Thanks, if this would be correct answer then you should always appreciate our efforts and provide Vote and Make as Correct Answer.
Abdi tombang 21-Feb-11 1:08am    
I've make the command become this :
Dim strCommand As String = ("Insert into Ruangan (Id_Ruangan, Nama_Ruangan, Jenis_Ruangan) values (‘ & id.Text & ’,‘" & nama.Text & "’,‘" & jenis.Text & "’)")

DataType of Id_Ruangan if it was Number and I've removed double quote from Me.id.Text, But it still give a same message error :(
Albin Abel 21-Feb-11 1:59am    
why those single quotes around the id.text still?

Dim strCommand As String = "Insert into Ruangan (Id_Ruangan, Nama_Ruangan, Jenis_Ruangan) values (" & id.Text & ",'" & nama.Text & "','" & jenis.Text & "')"

copy this line and try
A few things I can see, there is a connection opened at the load event. it is not closed, but again called open in button event...

If Id_Ruangan is a primary key it will not accept duplicates and if it is a autoincrement field you need not to insert it. Provide if any error details i can improve my answer here.

one more thing the command type is not given
 
Share this answer
 
v2
Comments
Abdi tombang 21-Feb-11 1:14am    
I've remove con.open() in form_load, but the message error still show with the same message :(

Id_Ruangan is a primary key but it's not autoincrement (I'm forget to make it, hehehe).
Thanks, I rate 4 for you :)
But still help me to solve this one :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900