i try to create an application that can sent the value from vb.net to SQL database..
the problem is i dont know how....i created the interface, just a simple one like inserting name, D.O.B, hobbies & phone nmber(all this using text area)...and a submit button...
pleaseeeeee........... help me :'(
-im still a padawan not a sith lord-

Recommended Answers

All 13 Replies

add this code to module to connected sqlserver with vb.net:

Imports System.Data
Imports System.Data.SqlClient

Module Koneksi
    Public conn As SqlConnection
    Public Function GetConnect()
        conn = New SqlConnection("server = MyServerName;database = MyDatabaseName;Trusted_Connection = yes")
        Return conn
    End Function
End Module

add this code to button event click :

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim check As Integer
        Dim conn As SqlConnection
        Dim cmdStudent As New SqlCommand
        Dim cmdStudent1 As New SqlCommand
        Dim daStudent As New SqlDataAdapter
        Dim dsStudent As New DataSet
        Dim dtStudent As New DataTable

        If txtId.text = "" Or txtFirstName.Text = "" txtLastName.Text = "" Or txtAge.Text = "" Then
            MsgBox("Student Data is not completed", MsgBoxStyle.OKOnly)
        Else
            If MsgBox("Are you sure to save Student data with Id : " & txtId.text & " ?", MsgBoxStyle.OKCancel, "Input confirm") = MsgBoxResult.Cancel Then
                ' do nothing
            Else
                Try
                    conn = GetConnect()
                    conn.Open()
                    cmdStudent = conn.CreateCommand
                    cmdStudent.CommandText = "SELECT * FROM Student WHERE Id='" & Trim(txtId.text) & " ' "
                    daStudent.SelectCommand = cmdStudent
                    daStudent.Fill(dsStudent, "Student")
                    dtStudent = dsStudent.Tables("Student")

                    If (dtStudent.Rows.Count > 0) Then
                        MsgBox("Student dengan Id " & Trim(cmbId.Text) & " already in database", MsgBoxStyle.OKOnly, "Message :")
                    Else
                        
                        cmdStudent1 = conn.CreateCommand
                        cmdStudent1.CommandText = "INSERT INTO Student(Id, FirstName, LastName,Age) VALUES('" & Trim(txtId.text) & "','" & Trim(txtFirstName.Text) & "','" & Trim(txtLastName.Text) & "','" & Trim(txtAge.Text) & "')"
                        check = cmdStudent1.ExecuteReader.RecordsAffected()
                        If check > 0 Then
                            MsgBox("Student With Id " & Trim(cmbId.Text) & " succesfully to added", MsgBoxStyle.OKOnly, "Message :")
                        Else
                            MsgBox("Student With Id " & Trim(cmbId.Text) & " Failure  to added", MsgBoxStyle.OKOnly, "Message :")
                        End If
                        Refresh_Form()
                        conn.Close()

                    End If

                Catch ex As Exception
                    MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OKOnly, "Connection Error !!")
                End Try
            End If
        End If
    End Sub

see that example and modified as you want.

commented: Nice code +1
commented: how great this code :) +1
commented: nice one... +1

//this is how i wrote the code but i cant still get to fix the error to execute it to query
on step 5

Public Class Form1
Inherits System.Windows.Forms.Form

Public varConnection As SqlConnection
Public varCommand As SqlCommand

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1") 'STEP 1
varConnection.Open() 'STEP 2
varCommand = New SqlCommand("insert into tbl_rop values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "',)", varConnection) 'STEP 3
varCommand.CommandTimeout = 9999 'STEP 4
varCommand.ExecuteNonQuery() 'STEP 5
varCommand.Dispose() 'STEP 6
varConnection.Close() 'STEP 7
End Sub


End Class

varCommand = New SqlCommand("insert into tbl_rop values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "',)", varConnection)

Wrong code, where u want to insert?what column Name of table?
eg :

cmdStudent1.CommandText = "INSERT INTO Student(Id, FirstName, LastName,Age) VALUES('" & Trim(txtId.text) & "','" & Trim(txtFirstName.Text) & "','" & Trim(txtLastName.Text) & "','" & Trim(txtAge.Text) & "')"

:) thaks i really appreciate the help...

:) thnks the code works fine..

you're Welcome...
Don't forget to mark this thread solved.
Happy Coding Friend :)

how to create next button that can travel to the next data located in the same sql server?

i have read ur thread on "how to connect to SQL server 2000?"..
in the thread u have teach how to create connection, save ,edit ,add and delete data. can u show how to search data from sql server n display the output in textbox?

pealse help me the login full code and steps vb2008 with sql 2000 database

I want to connect with two computer the soft wore so please by consider this help me the login code and steps

thankyou

how to connect two computers with vb2008 and sql2000 with user login form with code?

thankyou

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={SQL
Server};Server=server address;Database=databasename;UID=username;PWD=password;"


which page will i connect it

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
Conn.Open "DRIVER={SQL
Server};Server=server address;Database=databasename;UID=username;PWD=password;"


which page i connection that link tell me plz

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.