What do i do if i have code for insert.when i insert the data it shows when i click the SHOW button(when I enter a serial number and then click SHOW it shows the data) it works but when i stop the system and run it again it does not show the data and the data is not in the database.I am working with sql and vb.net 2008

Recommended Answers

All 3 Replies

If you put your code here, maybe we can see where the problem is.

Anyway, if you are usin a data adapter, maybe you forgot to call it for Update.
Otherwise, maybe you forgot to set the data adapter commands commands for insert, delete and update.

Hope this helps.

I am trying to use this code
I dont know what is wrong with this code it is not entering the data into the db.When you enter the data it reflects as having inserted the data into the db but when you stop the system and check the database the data is not in the db

'This class contains all the connection details and connection string
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web

Public Class frmConn

        Public Shared Function ConnSQL() As SqlConnection
        Dim connectionString As String
        Dim cnn As SqlConnection
        connectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\test_db.mdf;Integrated Security=True;User Instance=True"
        cnn = New SqlConnection(connectionString)
        Try
            cnn.Open()
            Return cnn
        Catch ex As Exception
            MsgBox("Can not open connection ! ")
        Finally
            cnn.Close()
        End Try
    End Function

Code:

'This class carries all the code for the transactions that will be carried out
'insert, delete, update
Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Net
Imports System.Net.Mail
Imports System.IO
Imports System.Text

Public Class ClassClass
    Public Shared Function SelectRows(ByVal query As String) As DataTable
        On Error Resume Next
        Dim Conn As New SqlConnection

        Conn = frmConn.ConnSQL

        Dim da As New SqlDataAdapter
        Dim dt As New DataTable
        dt.Clear()
        da.SelectCommand = New SqlCommand(query, Conn)
        da.Fill(dt)
        Return dt
    End Function
    Public Shared Sub SQL_Insert(ByVal sql_Insert1 As String, ByVal sql_Insert2 As String)
        Dim Conn As SqlConnection

        Conn = frmConn.ConnSQL
        Conn.Close()
        Conn.Open()
        Dim sqlcmd_Insert As New SqlCommand(sql_Insert1 + sql_Insert2, Conn)

        Try

            sqlcmd_Insert.ExecuteNonQuery()
            Catch ex As Exception
            Finally
            Conn.Close()
        End Try
    End Sub

    Public Shared Sub SQL_Update(ByVal sql_Update As String)
        Dim Conn As SqlConnection
        Conn = frmConn.ConnSQL
        Conn.Close()
        Conn.Open()
        Dim sqlcmd_Update As New SqlCommand(sql_Update, Conn)
        Try
            sqlcmd_Update.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Conn.Close()
        End Try
    End Sub
    Public Shared Sub Delete_Str(ByVal sql_Delete As String)
        Dim Conn As SqlConnection
        Conn = frmConn.ConnSQL
        Conn.Close()
        Conn.Open()

        Dim sqlcmd_Delete As New SqlCommand(sql_Delete, Conn)
        Try
            sqlcmd_Delete.ExecuteNonQuery()
            MsgBox("The Selected Record has been Deleted.")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class

code:

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
'This runs the insert sub
        insert_Items()
        End Sub

    Sub insert_Items()
'this sub contains the insert code
        Dim sql_insert1 As String = "INSERT INTO items_table (item )"
        Dim sql_insert2 As String = "VALUES ('" & Me.txtItem.Text & "')"
        ClassClass.SQL_Insert(sql_insert1, sql_insert2)
        MsgBox("Inserted Successfully")
    End Sub
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1


 ' Declare objects...
    Dim objConnection As New SqlConnection _
    ("server= localhost;database= MSS;user id= sa;password=clement;")
    Dim objDataSet As DataSet
    Dim objDataView As DataView
    Dim objCurrencyManager As CurrencyManager
'create object to handle SQLcommands
            Dim objcommand As SqlCommand = New SqlCommand

            With objcommand
                .Connection = objConnection
                .CommandText = "INSERT INTO TableName" & _
               "(ColumnName)" + _
                "VALUES (textbox1.text)"
            End With

            objConnection.Open()
            objcommand.ExecuteNonQuery()
            objConnection.Close()

            'prompt to alert the user on successful record entry 
            Dim alert As DialogResult = _
                    MessageBox.Show( _
                     "entry successfully:" + vbCrLf & _
                     "Do you wish to enter another?", _
                        "Midwife", MessageBoxButtons.YesNo, _
                            MessageBoxIcon.Question)

            'case statement to capture user response and act accordingly 
            Select Case alert
                Case Windows.Forms.DialogResult.Yes
                    ClearControls()
                Case Windows.Forms.DialogResult.No
                    Me.Dispose()
            End Select

        Catch ex As Exception
            MessageBox.Show("registration failed!" & vbCrLf & _
            ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.