Insert Command Problem

cimpercee

New member
Joined
Aug 7, 2007
Messages
2
Programming Experience
Beginner
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
 
Last edited:
You have .NET 2. DOnt do your data access that way. Read the PQ link in my signature, then read the DW2 link, section "Creating a SImple Data App"
 
If you were hammering a nail into the wall using a rock, and I told you to go and get this tool (called a Hammer) that you had never seen before to do the work, you would probably question that too.. But trust me, I'd tell you that like I tell you this: because youre doing it wrong now, and how I describe is how to do it right

I know that you understand this old way of working with databases, but that doesnt mean you shouldnt update your knoweldge and become a better coder in the process.. Take 15 mintues out to read the links and youll see just how hard work the old way is
 
yea, i was working the old way when i first joined here cjard taught me how, it was pretty simple and i figured out in prolly an hour or 2 saved me lots of troubles..

read the DW2 link on cjard signature too

It might be hard to adapt, yes i know cos i went thru that process too, but hey, thats the way u learn new stuff, u cant be sticking to .net 1.1 codes when .net 3.0 is released right?

trust me it is good, saves u time to code, and Easier to debug
 
Back
Top