insert record in multiple table using sqldataadapter and sqltransaction - sql

I'm trying to insert record in multiple table at a time using sqldataadapter and sqltransaction. i use the following code. but it is not working. help me plz....... tell me the problem in this code
Dim cn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=AMHSDB;Integrated Security=True")
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim trans As SqlTransaction
Dim tot As Integer
Public Sub RecordSave()
tot = CInt(TB3.Text.Trim) + CInt(TB4.Text.Trim) + CInt(TB5.Text.Trim) + CInt(TB6.Text.Trim)
Dim yr As String
yr = Now.Year()
Try
cn.Open()
trans = cn.BeginTransaction()
da.InsertCommand.Transaction = trans
da.InsertCommand = New SqlCommand("INSERT INTO[FTUT_tbl] (roll_no,s_name,class,session,eng_i,ben,math,stu_wor,gra_tot) VALUES ('" & TB1.Text.Trim & "','" & TB2.Text.Trim & "','Nursery','" & yr & "','" & TB3.Text.Trim & "','" & TB4.Text.Trim & "','" & TB5.Text.Trim & "','" & TB6.Text.Trim & "','" & tot & "')", cn)
da.InsertCommand.ExecuteNonQuery()
da.Dispose()
da.InsertCommand = New SqlCommand("INSERT INTO[FTE_tbl] (roll_no,s_name,class,session) VALUES ('" & TB1.Text.Trim & "','" & TB2.Text.Trim & "','Nursery','" & yr & "')", cn)
da.InsertCommand.ExecuteNonQuery()
da.Dispose()
trans.Commit()
MsgBox("Rocord Successfully Inserted!")
Catch ex As Exception
trans.Rollback()
MsgBox(ex.Message)
Finally
cn.Close()
End Try
End Sub

Create the InsertCommand first, and then assign the transaction:
da.InsertCommand = New SqlCommand("...")
da.InsertCommand.Transaction = trans
And you need to commit your transactions:
da.InsertCommand.Transaction.Commit()

try this, may be it will help for you
Dim dsBillMst As DataTable
Dim TrDtsSvr As SqlTransaction
Dim cmdReadLocal As New SqlCommand
Dim ConSvr As New SqlConnection(gblstrDBConnectionStr)
ConSvr.Open()
TrDtsSvr = ConSvr.BeginTransaction()
cmdReadLocal.Transaction = TrDtsSvr
cmdReadLocal.commandtext="//your insert statement"
cmdReadLocal.executenonquery()
adapter = New SqlDataAdapter(cmdReadLocal)
dsBillMst = New DataTable
adapter.Fill(dsBillMst)

your problem is you disposed the sql command in the first insert, but then you didnt open it again in another insert.
try this one.....
cn.Open()
trans = cn.BeginTransaction()
da.InsertCommand.Transaction = trans
da.InsertCommand = New SqlCommand("INSERT INTO[FTUT_tbl] (roll_no,s_name,class,session,eng_i,ben,math,stu_wor,gra_tot) VALUES ('" & TB1.Text.Trim & "','" & TB2.Text.Trim & "','Nursery','" & yr & "','" & TB3.Text.Trim & "','" & TB4.Text.Trim & "','" & TB5.Text.Trim & "','" & TB6.Text.Trim & "','" & tot & "')", cn)
da.InsertCommand.ExecuteNonQuery()
da.Dispose()
cn.Open()
da.InsertCommand = New SqlCommand("INSERT INTO[FTE_tbl] (roll_no,s_name,class,session) VALUES ('" & TB1.Text.Trim & "','" & TB2.Text.Trim & "','Nursery','" & yr & "')", cn)
da.InsertCommand.ExecuteNonQuery()
da.Dispose()

Related

Custom date format for inserting date in mysql database

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

When Inserting into database only two row in datagridview been save in vb.net

My problem here is every time i want to save into the database, only two row in the datagridview can be save. why is that ? is it because of the two command of inserting ?
Here is my code:
Try
For Each column As DataGridViewRow In dtorder.Rows
Dim sqlconn As New OleDb.OleDbConnection
Dim sqlquery As String
Dim sqlquery1 As String
Dim cmd As New OleDb.OleDbCommand
Dim cmd1 As New OleDb.OleDbCommand
Dim connString As String
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Bloodyjenk\Documents\POS system.accdb"
sqlconn.ConnectionString = connString
sqlconn.Open()
sqlquery = "INSERT INTO OrderedInfoName VALUES ('" & txtOrderNo.Text & "','" & column.Cells(0).Value & "','" & column.Cells(1).Value & "','" & column.Cells(2).Value & "','" & column.Cells(3).Value & "')"
sqlquery1 = "INSERT INTO Ordered VALUES ('" & txtOrderNo.Text & "','" & cmbTable.Text & "','" & cmbOrdertype.Text & "','" & lblDate.Text & "','" & lblStatus.Text & "','" & lblPrice.Text & "','" & lblDiscount.Text & "','" & lblSub.Text & "', 0, 0)"
cmd = New OleDb.OleDbCommand(sqlquery, sqlconn)
cmd1 = New OleDb.OleDbCommand(sqlquery1, sqlconn)
cmd.ExecuteNonQuery()
cmd1.ExecuteNonQuery()
sqlconn.Close()
Next
Catch ex As Exception
MsgBox("DONE")
OrderList.Show()
Me.Close()
End Try

Why error ???? Syntax error in INSERT INTO statement

Public Sub register_user()
Dim con As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\User\Documents\login.mdb")
con.Open()
Dim reg As New DataTable("loginadmin")
Dim aaa As New OleDb.OleDbDataAdapter("Select * from loginadmin ", con)
Dim rs As New OleDb.OleDbCommand("Insert into loginadmin(username,password) values ('" & TextBox1.Text & "', '" & TextBox2.Text & "')", con)
rs.ExecuteNonQuery()
MessageBox.Show("user successfully registerd")
con.Close()
End Sub
'debugger caught this error " syntax error insert into statement"
'the da
try this :- Dim rs As New OleDb.OleDbCommand ("Insert into loginadmin([username],[password]) values ('" & TextBox1.Text & "', '" & TextBox2.Text & "')", con)

Insert command VB does not actually insert data?

I have the following code which adds a record to the DB. The code executes fine, however when I look in the DB, there is no data added! Any help greatly appreciated:
Dim Con As SqlConnection
Dim cmd As New SqlCommand
Dim connstring As String
connstring = "Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Assignment.mdf;Integrated Security=True;Connect Timeout=30"
Con = New SqlConnection(connstring)
Con.Open()
cmd = New SqlCommand("Insert into Rota ([Id],[Monday],[Tuesday],[Wednesday],[Thursday],[Friday],[Saturday],[Sunday],[UserID]) Values('" & chosen & "','" & mon & "','" & tues & "','" & wed & "','" & thur & "','" & fri & "','" & sat & "','" & sun & "','" & user & "')", Con)
cmd.ExecuteNonQuery()
MsgBox("done")
Con.Close()

Saving CheckBox Items to Database Access

i have vb.net application form.
It contains ID, Age,Name as Textbox and TC as Checkbox.
I have following code to these items, but checkbox items is saved automatically whether checked or not.
So what to do?
[Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Dim con As New OleDbConnection
Dim cmd As New OleDbCommand
Dim con_str As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Satyam\Documents\Database2.accdb"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
con.ConnectionString = con_str
con.Open()
'MsgBox(con.State)
Dim myReader As OleDbDataReader
cmd = New OleDbCommand("select * from Table1", con)
myReader = cmd.ExecuteReader
While myReader.Read()
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
Private Sub AddTable1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddTable1.Click
Try
con.ConnectionString = con_str
con.Open()
'MsgBox(con.State)
cmd = New OleDbCommand("insert into Table1(ID,Age,Name,TC) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & Nametxt.Text & "','" & TCtxt.Text & "')", con)
cmd.ExecuteNonQuery()
MsgBox("Added Successfuly")
Dim myReader As OleDbDataReader
cmd = New OleDbCommand("select * from Table1", con)
myReader = cmd.ExecuteReader
Agetxt.Clear()
While myReader.Read()
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
End Class
Check this code. This is for 3 checkboxes. You can extend it for 10 checkboxes.
Have a look here:
Dim hbStr As String
Dim bgStr As String
Dim tcStr As String
If hb.Checked Then
hbStr = hb.Text
Else
hbStr = ""
End If
If bg.Checked Then
bgStr = bg.Text
Else
bgStr = ""
End If
If tc.Checked Then
tcStr = tc.Text
Else
tcStr = ""
End If
Dim cmd As New OleDbCommand
Dim conn As New OleDbConnection("Connection String")
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert into Table1(ID,Age,Name,TC,HB.BG) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & Nametxt.Text & "','" & tcStr & "','" & hbStr & "','" & bgStr & "')"
cmd.ExecuteNonQuery()
Hope this helps you.
Sorry it save only TC not Hb.
If Hbtxt.Checked Then
cmd = New OleDbCommand("insert into Table1(ID,Age,Hb) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & Hbtxt.Text & "')", con)
Else
cmd = New OleDbCommand("insert into Table1(ID,Age,Hb,) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','')", con)
End If
If TCtxt.Checked Then
cmd = New OleDbCommand("insert into Table1(ID,Age,TC) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','" & TCtxt.Text & "')", con)
Else
cmd = New OleDbCommand("insert into Table1(ID,Age,TC) values ('" & IDtxt.Text & "','" & Agetxt.Text & "','')", con)
End If