Hello Forum,
When I insert image data into the data table, the data is inserted twice. What could be the reason why data is inserted two times?
I set the Identity Specification of the ID column to true in my table, but I am seeing double data being inserted
Here is my insert code
protected void buttonmail_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 tableinvoice(email, Name, ContentType, Data, Uid, CreatedBy, Role, CreatedDate, Organization)" +
" VALUES(@email, @Name, @ContentType, @Data, @Uid, @CreatedBy, @Role, @CreatedDate, @Organization); SELECT @@IDENTITY";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@email", user.Text.Trim());
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Parameters.AddWithValue("@Uid", labelid.Text.Trim());
cmd.Parameters.AddWithValue("@CreatedBy", createby.Text.Trim());
cmd.Parameters.AddWithValue("@Role", role.Text.Trim());
cmd.Parameters.AddWithValue("@CreatedDate", DateTime.Now);
cmd.Parameters.AddWithValue("@Organization", named.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
object Invoice = cmd.ExecuteScalar();
con.Close();
Session["paperinv"] = Invoice;
Response.Redirect("Invoiceprint.aspx");
}
}
}
}
}