Thursday, October 16, 2008

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



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..

Update the datas in Oledb Provider

Trying to update an Access DB using Oledbconnection.

Getting a syntax error (missing operator) in the SQL statement below, cannot see what it is can anyone help?

DBCommand = New OleDbCommand("SELECT * FROM Customer Where CustID = " &_ LBL_CustID.Text & " UPDATE Customer SET CustName = " & TXT_CustName.Text & " ", ManufacturingConn)

Is there a better way to do it?

Sql Server 2005 Connection Code using VB.NET

Imports system.data.sqlclient
Public class Form1
dim con as new sqlconnection
dim cmd as new sqlcommand
dim dr as new datareader

public sub Button1_click()
Try
con = new sqlconnection("Type ur Connection String")
con.open()
cmd = new sqlcommand("Insert into table1 values ('" & TextBox1.Text &"','" & TextBox2.Text"')",con)
i=cmd.ExecuteNonquery()
msgbox("Record Inserted ",i)
Catch ex as Exception
End Try
con.open()
End Sub