hi i am facing problem with insertion of data into database
front end:- vb.net2003
backend:- SQL Server2000

please tell me code with which i can insert data in database

Recommended Answers

All 3 Replies

Hi Kartikey...
first : i wanna say welcome in dani web...
second : you didn't show any effort from your program..
"We only give homework help to those who show effort" :)
so, when you post thread again please show the effort...effort can make all of people here to understand the problem. thx :P...

if u want to insert or update you must to connect vb.net with sql server 2000.
for better (my opinion) u can use module so u can call every time in all form..
this code for connection module :

Imports System.Data
Imports System.Data.SqlClient  ' this is very important code that u must added 

Module Connect
    Public conn As SqlConnection
    Public Function GetConnect()
        conn = New SqlConnection("Server = YourServerName;" & "initial Catalog = YourDatabaseName;" & " Trusted_Connection=yes")
        Return conn
    End Function
End Module

this following code to insert data in database :

Dim check As Integer
    Dim conn As SqlConnection
    Dim cmdMedical As New SqlCommand
    Dim cmdMedical1 As New SqlCommand
    Dim daMedical As New SqlDataAdapter
    Dim dsMedical As New DataSet
    Dim dtMedical As New DataTable

                Try
                    conn = GetConnect() ' this when u call the module to connected....
                    conn.Open()
                    cmdMedical = conn.CreateCommand
                    cmdMedical.CommandText = "SELECT * FROM Medical WHERE Medical_No='" & Trim(txtMedical_No.SelectedItem) & " ' "
                    daMedical.SelectCommand = cmdMedical
                    daMedical.Fill(dsMedical, "Medical")
                    dtMedical = dsMedical.Tables("Medical")

                    If (dtMedical.Rows.Count > 0) Then
                        MsgBox("Patient With Medical_No " & Trim(txtMedical_No.Text) & " has been added in data base", MsgBoxStyle.OKOnly, "Message :")
                    Else
                        cmdMedical1 = conn.CreateCommand
                        cmdMedical1.CommandText = "INSERT INTO Medical(IdMedical, Medical_No, BloodType, Disease,BodyAnomaly,Tall,Weight) VALUES('" & Trim(idMedical.Text) & "','" & Trim(txtMedical_No.SelectedItem) & "','" & Trim(cmbBloodType.SelectedItem) & "','" & Trim(txtDisease.Text) & "','" & Trim(txtBodyAnomaly.Text) & "','" & Trim(txtTall.Text) & "','" & Trim(txtWeight.Text) & "')"
                        check = cmdMedical1.ExecuteReader.RecordsAffected()
                        If check > 0 Then
                            MsgBox("Patient With Medical_No " & Trim(txtMedical_No.Text) & " Succesfully Added", MsgBoxStyle.OKOnly, "Message :")
                        Else
                            MsgBox("Patient With Medical_No " & Trim(txtMedical_No.Text) & " failed to added", MsgBoxStyle.OKOnly, "Message :")
                        End If
                       
                        conn.Close()

                    End If

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

you can write the update code by your self and post a new thread if you get the problem..
ok..
all for the best..

NB : don't forget to reply the feedback

commented: nice greetings..:P +1

for insert code
first
this r import for any connection
At top write this
imports system.data
imports system.data.sqlclient
dim con as new sqlconnection(" "server=NOAHPROJECT;user id=test;password=test;database=test")
con.open()
dim cmd as new Sqlcommand("insert into tablename (name.address)values('"& txtname.text &"','"& txtaddress &" ' ",con)
cmd.excutenonqury()
-----------------------
for update
dim con as new sqlconnection(" "server=NOAHPROJECT;user id=test;password=test;database=test")
con.open()
dim cmd as new Sqlcommand("update tablename set name=' "& txtname &"',address='" & txtadress &"' ",con)
cmd.excutenonqury()

for insert code
first
this r import for any connection
At top write this
imports system.data
imports system.data.sqlclient
Then In insert Or add button write this code
dim con as new sqlconnection(" "server=NOAHPROJECT;user id=test;password=test;database=test")
con.open()
dim cmd as new Sqlcommand("insert into tablename (name.address)values('"& txtname.text &"','"& txtaddress &" ' ",con)
cmd.excutenonqury()
-----------------------
for update
In update button
dim con as new sqlconnection(" "server=NOAHPROJECT;user id=test;password=test;database=test")
con.open()
dim cmd as new Sqlcommand("update tablename set name=' "& txtname &"',address='" & txtadress &"' ",con)
cmd.excutenonqury()

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.