Jump to content

Add new record


amirag

Recommended Posts

Hi every body :) I wrote this code which adds new record in the database, it adds perfectly... but the problem here, it adds the row twice in the table. I don't konw why :) I think there's something wrong in the sequence of the code :)

<!-- #Include File="header.aspx" --><%@ Page Language="vb" Debug="true" %><%@ import Namespace="system.data.oleDb"%><script runat="server">Sub UploadButton_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Loadif session("adminId")<>"" then If IsPostBack Then	'/////////dim dbconn,dbcomm,sql,noAdded,strtitle,strdetails,strdate,fileupdbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.MapPath("/db/pscchc_db.mdb"))dbconn.open()strtitle=title.textstrdetails=details.textstrdate=ndate.textfileup=Server.MapPath("/UploadedImages/")& FileUpload1.FileNamesql="insert into news(title,details,ndate,img) VALUES ('" & strtitle & "','" & strdetails & "','" & strdate & "','" & fileup & "');"'create the command objectdbcomm= New OleDbCommand(sql,dbconn)'execute SQL commandnoAdded=dbcomm.executeNonQuery()msg.text="<br><br><font color=red>" & noAdded & "</font> rocord(s) has been Added"'////////////UPLOAD FILE/////////////////////   		Dim path As String = Server.MapPath("/UploadedImages/")		Dim fileOK As Boolean = False		If FileUpload1.HasFile Then			Dim fileExtension As String			fileExtension = System.IO.Path. _				GetExtension(FileUpload1.FileName).ToLower()			Dim allowedExtensions As String() = _				{".jpg", ".jpeg", ".gif"}			For i As Integer = 0 To allowedExtensions.Length - 1				If fileExtension = allowedExtensions(i) Then				   fileOK = True				End If			Next			If fileOK Then				Try					FileUpload1.PostedFile.SaveAs(path & _						 FileUpload1.FileName)					Label1.Text = "File uploaded!"				Catch ex As Exception					Label1.Text = "File could not be uploaded."				End Try			Else				Label1.Text = "Cannot accept files of this type."			End If		End If	End IfElseresponse.redirect("main_log.aspx")End if ' close If sessionEnd Sub</script><form runat="server"><table> <tr><td><b>Date: </b></td><td><asp:TextBox ID="ndate" CssClass="input-box"  runat="server"></asp:TextBox></td></tr><tr><td><b>Title:</b></td><td><asp:TextBox ID="title" CssClass="input-box" runat="server" Width="200"> </asp:TextBox></td><tr><td valign="top"><b>Details:</b></td><td><asp:TextBox ID="details" TextMode="MultiLine" Width="500" Rows="20"  runat="server"></asp:TextBox></td></tr><tr><td><b>Image: </b></td><td> </td></tr><Tr><td colspan="2" align="center"></td></Tr><tr><td colspan="2"><font color="#999999" size="2">Note: maximum image Dimensions are Width:347 * height:260<br></font><asp:FileUpload id="FileUpload1" runat="server"> </asp:FileUpload>			<asp:Label ID="Label1" runat="server"></asp:Label></td></tr><tr><td colspan=2><asp:Label ID="msg" runat="server"></asp:Label></td></tr><tr><td colspan="2"><asp:Button Text="ADD" runat="server" CssClass="ButtonFormat2" OnClick="UploadButton_Click"></asp:Button></td></tr></table></form><!-- #Include File="footer.aspx" -->

Any suggestion will be welcomeThanx in advance :blink:

Link to comment
Share on other sites

<script runat="server">Sub UploadButton_Click(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load....

I develop in C# rather than VB so I may be mistaken here, but it looks like you have "UploadButton_Click" set up to fire on both the OnClick event of the button and also on the OnLoad event of the Page. (Handles Me.Load).When you have an event like the OnClick event, the OnLoad event of the page fires first followed by the OnClick event. Since your event handler is set up to handle both the OnClick event and the OnLoad event of the Page, it's running twice.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...