Hi everyone. Im having problems on updating my table.
here is my code.im new to .net. I hope you could help me.
Thanks
Dim cnConnection As New OleDbConnection(DataAccess.GetConnectionString("MyConnection"))
Dim cmdCommand As New OleDbCommand()
Dim daAdapter As New OleDbDataAdapter
Dim dtSet As New DataTable
With cmdCommand
.CommandText = ("Insert Into Accounts (Username,Password) Values ('"+txtuname.text+"','"+txtpword.text+"')")
.CommandType = CommandType.Text
.Connection = cnConnection
End With
cnConnection.Open()
cmdCommand.ExecuteNonQuery()
Dim sbinding As BindingSource = DataAccess.GetSetBindingSource(cmdCommand)
cnConnection.Close()
My DataAccess Class
Imports System.Data.OleDb
Imports System.Configuration
Imports System.Configuration.ConfigurationSettings
Public Class DataAccess
'This returns the connection string
Public Shared Function GetConnectionString(ByVal strConnection As String) As String
Dim strReturn As New String("")
If Not String.IsNullOrEmpty(strConnection) Then
strReturn = ConfigurationManager.ConnectionStrings(strConnection).ConnectionString
Else
strReturn = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString
End If
Return strReturn
End Function
Public Shared Function GetSetBindingSource(ByVal cmdSql As OleDbCommand) As BindingSource
Dim oBindingSource As New BindingSource()
' Create a new data adapter based on the specified query.
Dim daGet As New OleDbDataAdapter(cmdSql)
' Populate a new data table and bind it to the BindingSource.
Dim dtGet As New DataTable()
cmdSql.CommandTimeout = 240
dtGet.Locale = System.Globalization.CultureInfo.InvariantCulture
Try
daGet.Fill(dtGet)
Catch ex As Exception
MsgBox("Please notify the Helpdesk of this error:" + vbCrLf + ex.Message + vbCrLf + "Error in amaDBHelper.GetBindingSource")
Return Nothing
End Try
oBindingSource.DataSource = dtGet
Return oBindingSource
End Function
End Class
my error is on the commandtext. Is my string correct?if i code it like this
("Insert Into Accounts (Username) Values ('"+txtuname.text+"')")
the table is updating, only if i put mulitiple columns and multiple values just like the code above.Also i get error on executenonquery
here is my code.im new to .net. I hope you could help me.
Thanks
Dim cnConnection As New OleDbConnection(DataAccess.GetConnectionString("MyConnection"))
Dim cmdCommand As New OleDbCommand()
Dim daAdapter As New OleDbDataAdapter
Dim dtSet As New DataTable
With cmdCommand
.CommandText = ("Insert Into Accounts (Username,Password) Values ('"+txtuname.text+"','"+txtpword.text+"')")
.CommandType = CommandType.Text
.Connection = cnConnection
End With
cnConnection.Open()
cmdCommand.ExecuteNonQuery()
Dim sbinding As BindingSource = DataAccess.GetSetBindingSource(cmdCommand)
cnConnection.Close()
My DataAccess Class
Imports System.Data.OleDb
Imports System.Configuration
Imports System.Configuration.ConfigurationSettings
Public Class DataAccess
'This returns the connection string
Public Shared Function GetConnectionString(ByVal strConnection As String) As String
Dim strReturn As New String("")
If Not String.IsNullOrEmpty(strConnection) Then
strReturn = ConfigurationManager.ConnectionStrings(strConnection).ConnectionString
Else
strReturn = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString
End If
Return strReturn
End Function
Public Shared Function GetSetBindingSource(ByVal cmdSql As OleDbCommand) As BindingSource
Dim oBindingSource As New BindingSource()
' Create a new data adapter based on the specified query.
Dim daGet As New OleDbDataAdapter(cmdSql)
' Populate a new data table and bind it to the BindingSource.
Dim dtGet As New DataTable()
cmdSql.CommandTimeout = 240
dtGet.Locale = System.Globalization.CultureInfo.InvariantCulture
Try
daGet.Fill(dtGet)
Catch ex As Exception
MsgBox("Please notify the Helpdesk of this error:" + vbCrLf + ex.Message + vbCrLf + "Error in amaDBHelper.GetBindingSource")
Return Nothing
End Try
oBindingSource.DataSource = dtGet
Return oBindingSource
End Function
End Class
my error is on the commandtext. Is my string correct?if i code it like this
("Insert Into Accounts (Username) Values ('"+txtuname.text+"')")
the table is updating, only if i put mulitiple columns and multiple values just like the code above.Also i get error on executenonquery
Last edited: