Sunday, February 19, 2012

Inserting Records

pls i have this issue on inserting records into SQL SERVER Express. this is the code so far :-

imports system.data

imports system.data.sqlclient

Protected

Sub btn1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles btn1.ClickDim strConnAsString = ConfigurationManager.ConnectionStrings("Connect1").ConnectionStringDim ObjConnAsNew SqlConnection(strConn)Dim strCommAsString ="insert Products(ProductName,UnitPrice) values(@.ProductName,@.UnitPrice)"Dim ObjCommAsNew SqlCommand(strComm, ObjConn)

ObjComm.Parameters.AddWithValue(

"@.ProductName", txtprodname.Text)

ObjComm.Parameters.AddWithValue(

"@.UnitPrice", SqlDbType.Money).Value = txtprice.Text

ObjConn.Open()

ObjComm.ExecuteNonQuery()

ObjConn.Close()

EndSub

End

Class

The issue i have is that anytime i click the button to insert a new record, i get two records added to the database automatically

What does the declaration for btn1_Click look like on your .aspx page?|||

tmorton:

What does the declaration for btn1_Click look like on your .aspx page?

pls i dont understand what u mean by declaration for btn1_Click. PLs elaborate.

thanks

|||Sorry, I am trying to ask what the <asp:button declaration looks like for btn1.|||

tmorton:

Sorry, I am trying to ask what the <asp:button declaration looks like for btn1.

Do U mean the On_Click event handler ? If so i used it as

<asp:Button ID="btn1" runat="server" OnClick="btn1_Click"/> if u are still not satisfied with these please recommend a site where i can have a guide or u please help withsome sample codes

|||

websyd:

Do U mean the On_Click event handler ? If so i used it as

<asp:Button ID="btn1" runat="server" OnClick="btn1_Click"/> if u are still not satisfied with these please recommend a site where i can have a guide or u please help withsome sample codes


Yes, that's what I meant. You basically have wired up the Click event twice, as you have it present in your Button declaration, as well as in your "Handles" clause. See Mike Pope's blog post for a bit more explanation:Documenting Event Handlers. You need to remove it from one place or the other, then the code will only run one time. I'd probably just remove the Handles clause from this code:

Sub btn1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles btn1.Click|||Great Guy, U hav made me happy with this forum again. It worked perfectly OK. Thanks i hope i can always right u to solve my problems as regards ASP.NET

No comments:

Post a Comment