locked
Data is Inserted twice into database on a single button click RRS feed

  • Question

  • User-1994446809 posted

    hello forum,

    Please I need help in resolving this issue I am facing in my project. when I try to insert data into table on a single button click the inserted data is double.  The data to be inserted is a PDF file displayed in canvas control. when PDF file is uploaded into the canvas using the fileupload control, it displays the file in the canvas on the web form.

    Please I need help in resolving this. Thank you

    Here is my Table Schema. The Id column is set to Identity true

    Id

    Int

    Email

    Nvarchar(MAX)

    ContentType

    Nvarchar(MAX)

    Image

    Varbinary(MAX)

    Createddate

    Nvarchar(MAX)

    This is my HTML where I have fileupload control, canvas and a button controls

     <div class="mid-cont col-sm">
                           <div class="containr-fluid">
                               <asp:FileUpload runat="server" ID="showPreviewBill" />
                               <br />
                               <br />
                           </div>
                           <div class="contentt">
                               <canvas id="the-canvas" style="border: 1px solid grey; height: 750px; width: 650px;"></canvas>
                           </div>
                            <asp:Button ID="insertbutton" runat="server" CssClass="btn btn-primary navbar-btn" BackColor="SteelBlue" Font-Size="9pt" Text="QR code" OnClick="insertbutton_Click" />
                       </div>

    This is the JavaScript that controls the fileupload and uploads the file into the canvas

     <script type="text/javascript">
            $(function () {
                $("#showPreviewBill").change(function () {
                    if (this.files && this.files[0]) {
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            showInCanvas(e.target.result);
                        }
                        reader.readAsDataURL(this.files[0]);
                    }
                });
     
                function convertDataURIToBinary(dataURI) {
                    var BASE64_MARKER = ';base64,';
                    var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
                    var base64 = dataURI.substring(base64Index);
                    var raw = window.atob(base64);
                    var rawLength = raw.length;
                    var array = new Uint8Array(new ArrayBuffer(rawLength));
     
                    for (i = 0; i < rawLength; i++) {
                        array[i] = raw.charCodeAt(i);
                    }
                    return array;
                }
     
                function showInCanvas(url) {
                    'use strict';
                    var pdfAsArray = convertDataURIToBinary(url);
                    pdfjsLib.getDocument(pdfAsArray).then(function (pdf) {
                        pdf.getPage(1).then(function (page) {
                            var scale = 1.5;
                            var viewport = page.getViewport(scale);
                            var canvas = document.getElementById('the-canvas');
                            var context = canvas.getContext('2d');
                            canvas.height = viewport.height;
                            canvas.width = viewport.width;
                            var renderContext = {
                                canvasContext: context,
                                viewport: viewport
                            };
                            page.render(renderContext);
                        });
                    });
                }
            });
        </script>

    This is C# code that inserts the file into database table

     protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True";
            if (!string.IsNullOrEmpty(Request.QueryString["receipt"]))
            {
                Session["rect"] = Convert.ToInt32(Request.QueryString["rect"]);
            }
    
        }
    
     protected void insertbutton_Click(object sender, EventArgs e)
        {
                string filename = Path.GetFileName(showPreviewBill.PostedFile.FileName);
                string contentType = showPreviewBill.PostedFile.ContentType;
                using (Stream fs = showPreviewBill.PostedFile.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        byte[] bytes = br.ReadBytes((Int32)fs.Length);
                        using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
                        {
                            string query = "INSERT INTO table(email, ContentType, image, Createddate)" +
                        " VALUES(@email, @ContentType, @image, @Createddate); SELECT @@IDENTITY";
                            using (SqlCommand cmd = new SqlCommand(query))
                            {
                                cmd.Connection = con;
                                cmd.Parameters.AddWithValue("@email", user.Text.Trim());
                                cmd.Parameters.AddWithValue("@ContentType", contentType);
                                cmd.Parameters.AddWithValue("@image", bytes);
                                cmd.Parameters.AddWithValue("@Createddate", DateTime.Now);
                                con.Open();
                                cmd.ExecuteNonQuery();
                                object rect = cmd.ExecuteScalar();
                                con.Close();
                                Session["rect"] = rect;
                                Response.Redirect("Print.aspx");
                            }
                        }
                    }
                }
        }

    Sunday, December 13, 2020 9:12 PM

Answers

All replies