i have written this code to inser data in my database table "member "through the form but it is not working if i start debugging the code it gives no error but when i open my database table to see the inserted data it is not working.
please help me to do this succesfully.

Imports System.Data.SqlClient
Public Class Form3
    Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If con.State = ConnectionState.Open Then
            con.Close()
        End If

        Try
            If RichTextBox1.Text.Length = 0 Then
                MsgBox("MemId field cannot be empty insert value")
            Else
                con.Open()

                Dim instr As String = "INSERT INTO member VALUES('" + RichTextBox1.Text + "','" + RichTextBox2.Text + "','" + RichTextBox3.Text + "','" + RichTextBox4.Text + "','" + RichTextBox5.Text + "','" + RichTextBox6.Text + "')"
                Dim cmd As New SqlCommand(instr, con)
                MsgBox("Database connected successfully")
                Dim rdr = cmd.ExecuteReader()
                If rdr.HasRows Then
                    Label8.Text = "*Record created successfully*"
                    cmd.Dispose()
                End If
                rdr.Close()
                cmd.Dispose()

            End If
        Catch ex As Exception
            MsgBox(ex.Message)
            con.Close()

        End Try

    End Sub

Recommended Answers

All 5 Replies

Dim rdr = cmd.ExecuteReader()

I dont think that you can execute an INSERT with ExecuteReader()
There should be another command like cmd.Execute()

For an execute reader, you need a SELECT statement.

>it gives no error but when i open my database table to see the inserted data it is not working.

Connection string with |DataDirectory| will write any changes at database under the Bin\Debug.

>it gives no error but when i open my database table to see the inserted data it is not working.

Connection string with |DataDirectory| will write any changes at database under the Bin\Debug.

yeah you are asolutely right.but fortunately i have solved my problem my own. it feels gr8.:):icon_biggrin:
and you people could not help me to solve my problem.:icon_redface:

Dim rdr = cmd.ExecuteReader()

I dont think that you can execute an INSERT with ExecuteReader()
There should be another command like cmd.Execute()

For an execute reader, you need a SELECT statement.

hello before posting please try to run this code . It will run without any error and smoothly dear.:icon_cool:
and for your kind information i have solved my problem.please try it yourself to make this code working without changing any word from this code.only the connection string you can change but i thik it won't need it .
if you can't then ask me i'll tell you how it will run.
all the best.:icon_razz:

>yeah you are asolutely right.but fortunately i have solved my problem my own. it feels gr8

I am so glad to hear that! Thanks.

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.