Hi,
Im using ASP.NET and SQL Server. While inserting the data at run time. the same data , what ever is already in database is being stored again. Say for example, i have empname and empno, "Mala" and 123 already stored in database, if i give new data "Bala" and 124, as soon as i click in insert button, the data's are stored. but Mala and 123 is stored . Even i f i give anyother data. 4 or 5 data i store, again 4 or 5 times the same data repeatedly being stored.

i.e. Mala and 123 is in database for 5 to 6 times. i donno whats wrong with my code.

My code is

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb

Inherits System.Web.UI.Page
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim cmb As SqlCommandBuilder
    Dim adp As SqlDataAdapter
    Dim data As DataSet3
    Dim dr As DataRow
    Dim selct, uptext As String

In Page_Load

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            con = New SqlConnection("Data Source=ACER;Initial Catalog=preethi;Integrated    Security=True")
            con.Open()
            selct = "select * from cooldrinks"
           adp = New SqlDataAdapter(selct, con)
            data = New DataSet3()
            adp.Fill(data, "cooldrinks")
            fillcontrols()
      Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

Private Sub fillcontrols()
        TextBox1.Text = data.Tables(0).Rows(0).Item(0)
        TextBox2.Text = data.Tables(0).Rows(0).Item(1)
        TextBox3.Text = data.Tables(0).Rows(0).Item(2)
    End Sub


Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        clearcontrols()
    End Sub


Private Sub clearcontrols()
        TextBox1.Text = " "
        TextBox2.Text = " "
        TextBox3.Text = " "
        TextBox4.Text = " "
        TextBox1.Focus()
End Sub


Protected Sub Insert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Ssql As String
        Ssql = "insert into cooldrinks(drinkname,color,price)values(@drinkname,@color,@price)"
        cmd = New SqlCommand(Ssql, con)
        cmd.Parameters.AddWithValue("@drinkname", TextBox1.Text)
        cmd.Parameters.AddWithValue("@color", TextBox2.Text)
        cmd.Parameters.AddWithValue("@price", TextBox3.Text)
        cmd.ExecuteNonQuery()
        Response.Write("One Record inserted Successfully")

    End Sub

Can any one please help me , to solve this?

do your textboxes have a default value of "mala" and "123"?

May be multiple clicks on the button leads to create repeated data in table.You have to redirect current page itself after insertion occurs, or else you have to check record exists in table by select query before insertion...

May be multiple clicks on the button leads to create repeated data in table.You have to redirect current page itself after insertion occurs, or else you have to check record exists in table by select query before insertion...

once executenonquery command is executed clean ur textbox value,and in insert function before inserting data check any one textbox value which is required, that it is blank or not,if blank than exit from function.

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.