Solved

Add/Update/Delete Record in VB.NET

Posted on 2008-10-23
6
3,766 Views
Last Modified: 2012-08-13
How do I Add/update a record in VB.NET for a table that does not have a primary key ? I want to update the record without using the SqlCommand.
0
Comment
Question by:msbhatt1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 22787876
Do you mean without using the sqlcommandbuilder?  You can set your insert/update SQL as the sqlcommand.text and then use sqlcommand.exectuenonquery to send it to your database.
0
 
LVL 41

Accepted Solution

by:
graye earned 250 total points
ID: 22798889
That's gonna be tough...   Using just the normal ADO.Net, you can perform an Insert into a table without a primary key, but you'd need a primary key to identify the desired row in order to perform an Update or Delete operation on that row.  Recall that ADO.Net inherently uses a "disconnected model" (which is the part of the puzzle that requires a primary key).
An easy alternative would be to use the older "classic" ADO... since it's "connected" model has no such requirement for a primary key.
0
 
LVL 3

Assisted Solution

by:Mezillinu
Mezillinu earned 250 total points
ID: 22916113
Hi, the following are examples, on how to INSERT, UPDATE and DELETE using SQL from VB. NET

When deleting, or updating, you can use names, but it is not the recommended way of updating and deleting. the recommended way is to have an ID which is unique. in names, you can have duplicates, which could lead of you deleting not the intended one.

so for future practise, its better to stick to primary keys with unique ID's, so you use them when updating and deleting.

cheers
ADDING
 
 Try
            Dim SQL_Connection As New SqlClient.SqlConnection
            Dim SQL_Command As New SqlClient.SqlCommand
            Dim sSQL_Command As String = ""
 
            Dim tools As New Tools
            Dim databaseConnectionString As String = tools.getXmlValue("DBConnStringErrorHandlingException")
            SQL_Command.Connection.ConnectionString = databaseConnectionString
 
            'create the insert 
            sSQL_Command = "INSERT INTO tbl_EmailTemplates ( " & _
                           "     [TemplateName]        " & _
                           "   , [TemplateDescription]        " & _
                           "   , [TemplateContent]          " & _
                           ") " & _
                           "VALUES ( " & _
                           "     @TemplateName        " & _
                           "   , @TemplateDescription        " & _
                           "   , @TemplateContent          " & _
                                                ")"
 
            With SQL_Command
                .Parameters.AddWithValue("@TemplateName", Me.txtTemplateName.Text)
                .Parameters.AddWithValue("@TemplateDescription", Me.txtDescription.Text)
                .Parameters.AddWithValue("@TemplateContent", Me.htmlEmailTemplateEditor.Value.ToString)
                .CommandText = sSQL_Command
            End With
 
            SQL_Connection.Open()
            SQL_Command.ExecuteNonQuery()
            SQL_Connection.Close()
        Catch ex As Exception
            Me.lblStatus.Text = ex.Message
        End Try
        
 
DELETING - USING OLE DB, CHANGE AS ABOVE
 
Try
 
                Dim SQL_Connection As New OleDbConnection
                Dim SQL_Command As New OleDbCommand
                Dim sSQL_Command As String = ""
 
                Dim tools As New Tools
                Dim databaseConnectionString As String = tools.getXmlValue("DBConnStringErrorHandlingException")
                SQL_Connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & databaseConnectionString
                SQL_Command.Connection = SQL_Connection
 
                'create the insert 
                sSQL_Command = "delete from tbl_emailTemplates where templatename = @templatename"
 
                With SQL_Command
                    .Parameters.AddWithValue("@TemplateName", Me.txtTemplateName.Text)
                    .CommandText = sSQL_Command
                End With
 
                SQL_Connection.Open()
                SQL_Command.ExecuteNonQuery()
                SQL_Connection.Close()
                refreshApp()
            Catch ex As Exception
                Me.lblStatus.Text = ex.Message
            End Try
 
UPDATING - ALSO USING OLE DB, CHANGE AS SQL CLIENT'S SQLCOMMAND AND SO ON
 
 Try
            Dim SQL_Connection As New OleDbConnection
            Dim SQL_Command As New OleDbCommand
            Dim sSQL_Command As String = ""
 
            Dim tools As New Tools
            Dim databaseConnectionString As String = tools.getXmlValue("DBConnStringErrorHandlingException")
            SQL_Connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & databaseConnectionString
            SQL_Command.Connection = SQL_Connection
 
            'create the insert 
            sSQL_Command = "update tbl_EmailTemplates " & _
                           "  set TemplateContent = @templatecontent " & _
                           " where TemplateName = @templatename"
 
            With SQL_Command
                .Parameters.AddWithValue("@TemplateName", Me.txtTemplateName.Text)
                .Parameters.AddWithValue("@TemplateContent", Me.htmlEmailTemplateEditor.Value.ToString)
                .CommandText = sSQL_Command
            End With
 
            SQL_Connection.Open()
            SQL_Command.ExecuteNonQuery()
            SQL_Connection.Close()
 
            SQL_Command.Dispose()
            SQL_Connection.Dispose()
        Catch ex As Exception
            Me.lblStatus.Text = ex.Message
 
        End Try

Open in new window

0
 
LVL 3

Expert Comment

by:Mezillinu
ID: 22916118
note that in the above examples, the only one using SQL, is the first insertion, so you have to convert the ole db command to sql, like in the first example.

its not so hard, you should cope :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question