Hi all..
I hope someone here can help me to solve my problem...I try to serach and get the solution from google..but it not help much...

when I click button on page..the data will inserted twice in my database...
i using select and insert statement in same connection...

also have executeScalar() and executeNonQuery() at the same time...

i using executeScalaer for get the data from column..
and executeNon Query to insert into database..

i hope somebody willing to see my coding here...

Private Function TambahRekod(ByVal sNama As String, ByVal sAlmt1 As String, ByVal sAlmt2 As String, ByVal sAlmt3 As String, ByVal sPoskod As String, ByVal sBdr As String, ByVal sNgri As String, ByVal sNBank As String, ByVal sNoABnk As String, ByVal sAlmtBnk1 As String, ByVal sAlmtBnk2 As String, ByVal sAlmtBnk3 As String, ByVal sPoskodBnk As String, ByVal sBdrBnk As String, ByVal sNgriBnk As String) As String

        Dim sSamb As String = ConfigurationManager.ConnectionStrings("cons_skew").ConnectionString
        Dim odbcSamb As New OdbcConnection(sSamb)

        Dim sQuery1 As String = "SELECT MAX(wc15kodplgn)FROM wc15fiplgnu WHERE (wc15kodplgn LIKE 'LP%')"

        odbcSamb.Open()
        Dim odbcArahan As New OdbcCommand(sQuery1, odbcSamb)
        Dim skodplgn As String = odbcArahan.ExecuteScalar()

        skodplgn = skodplgn.Substring(2)
        Dim skodplgn3 As Int32 = Convert.ToInt32(skodplgn) + 1
        Dim skodplgn2 As String = "LP" + skodplgn3.ToString()

        lblTest1.Text = skodplgn2

        Try
            Dim sQuery As String = "INSERT INTO wc15fiplgnu (wc15kodplgn, wc15nama, wc15almt1, wc15almt2, wc15almt3, wc15poskod, wc15bandar, wc15negeri, wc15namabank, wc15noakbank, wc15albnk1, wc15albnk2, wc15albnk3, wc15pkdbnk, wc15bdrbnk, wc15ngrbnk) VALUES ('" + skodplgn2 + "','" + sNama + "','" + sAlmt1 + "','" + sAlmt2 + "','" + sAlmt3 + "','" + sPoskod + "','" + sBdr + "','" + sNgri + "','" + sNBank + "','" + sNoABnk + "','" + sAlmtBnk1 + "','" + sAlmtBnk2 + "','" + sAlmtBnk3 + "','" + sPoskodBnk + "','" + sBdrBnk + "','" + sNgriBnk + "')"
            Dim odbcArah1 As New OdbcCommand(sQuery, odbcSamb)
            odbcArah1.ExecuteNonQuery()


        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine("[Pendaftaran Pembekal] Ralat " & ex.Message)
        End Try
        odbcSamb.Close()
        Return True

    End Function

The exectuteNonQuery is only being called once so the only thing that could be causing the duplication is that your function TambahRekod() is being called twice. What calls the function initially?

Well, it could be due to many reason, check your button (if any, that is calling this funtion), "Cause Validation" property is true or false, try to make it false and see the effetct, check how many times your page life cycle is executing by debugging.

The exectuteNonQuery is only being called once so the only thing that could be causing the duplication is that your function TambahRekod() is being called twice. What calls the function initially?

i just called the function TambahRekod() once in the sub page_load()

TambahRekod(txtNama.Text, txtAlmt1.Text, txtAlmt2.Text, txtAlmt3.Text, txtPoskod.Text, txtBandar.Text, ddNegeri.SelectedItem.Text, ddNamaBank.SelectedItem.Text, txtNoAkaunBank.Text, txtAlmtBank1.Text, txtAlmtBank2.Text, txtAlmtBank3.Text, txtPoskodBank.Text, txtBandarBank.Text, ddNegeriBank.SelectedItem.Text)

Well, it could be due to many reason, check your button (if any, that is calling this funtion), "Cause Validation" property is true or false, try to make it false and see the effetct, check how many times your page life cycle is executing by debugging.

i already check "Cause Validation" and it's set by default 'True',and i try to changed to 'False' but still same..not have any effect at all..

i figure out...that maybe because i call execute twice..one executescalar() and executenonquery()..but if i not using this method,how can i solve this problem because i need the data...

If you have the initial call in the page_load event it is running twice. Once when the page loads and again when the controls are redrawn after entering text.
You need to call the function inside of this code:

if Not IsPostBack then
   TambahRekod()
end if

This will only call the code when the page is first loaded. Page reloads ignore it.

If you have the initial call in the page_load event it is running twice. Once when the page loads and again when the controls are redrawn after entering text.
You need to call the function inside of this code:

if Not IsPostBack then
   TambahRekod()
end if

This will only call the code when the page is first loaded. Page reloads ignore it.

I not initial call this function on page_load...but in btnTambah_Click
here is coding

Protected Sub btnTambah_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTambah.Click

        If (txtNama.Text = "" Or txtAlmt1.Text = "" Or txtPoskod.Text = "" Or txtBandar.Text = "" Or txtNoAkaunBank.Text = "") Then
            lblMsj.Text = "Rekod gagal didaftar"
            lblMsj.ForeColor = System.Drawing.Color.Red
        Else
            //Initial function TambahRekod()
            TambahRekod(txtNama.Text, txtAlmt1.Text, txtAlmt2.Text, txtAlmt3.Text, txtPoskod.Text, txtBandar.Text,     
            ddNegeri.SelectedItem.Text,ddNamaBank.SelectedItem.Text, txtNoAkaunBank.Text, txtAlmtBank1.Text, txtAlmtBank2.Text, 
            txtAlmtBank3.Text, txtPoskodBank.Text, txtBandarBank.Text, ddNegeriBank.SelectedItem.Text)

            lblMsj.Text = "Rekod berjaya didaftar"
        End If

End Sub

3 posts ago you said you did call it in the page_load function. Was that a typo?

3 posts ago you said you did call it in the page_load function. Was that a typo?

yup..sorry..i forget about that...:)

Because if you do call the function in the page_load and don't use the Not IsPostBack check it will get called on a page reload as well.

Because if you do call the function in the page_load and don't use the Not IsPostBack check it will get called on a page reload as well.

But I not called the function in page_load...process done when I click button..

...Initial function on btnTambah_Click() not in Page_load()

Do one thing, send us the complete cs file. remove all other code that doesn't concern it so that we can read it fast. Also remove all sensative info for your security.

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.