In one of my recent questions on saving a signature from canvas, I was taught how to insert a signature into database table.
Insert (Save) signature from canvas into database using C# and VB.Net
However, it is different from what I want to achieve on this one.
Currently I have other data values that I want to insert as well. I created a form where users can type their data in sign a signature and on button click, the text data and signature is inserted into database.
Is it possible to do this without using a Web Method and jQuery?
Here is my insert code it also inserts image data, so I was thinking if there is no way to insert the signature as byte just like image is inserted?
In the code below, I commented on a particular line. The comment is intended to be a clue to explain what I really mean.
HTML
<div class="dsign">
<asp:Label ID="namesign" runat="server" Font-Bold="true" Font-Size="11pt" Text=""></asp:Label>
<canvas id="colors_sketch" runat="server" width="300" height="110" style="border: 1px solid #ccc"></canvas>
<script src="https://cdn.rawgit.com/mobomo/sketch.js/master/lib/sketch.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$('#colors_sketch').sketch();
$(".tools a").eq(0).attr("style", "color:#000");
$(".tools a").click(function () {
$(".tools a").removeAttr("style");
$(this).attr("style", "color:#000");
});
});
</script>
<div class="tools">
<a href="#colors_sketch" style="color: #145c7c;" data-tool="marker">Sign</a> <a href="#colors_sketch" style="color: red;" data-tool="eraser">Erase</a>
</div>
</div>
C# (Insert Code)
protected void Button2_Click(object sender, EventArgs e)
{
//byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]);
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True");
try
{
byte[] image;
Stream str = colors_sketch;//Here instead of having the name of image file control, the canvas can be used.
BinaryReader bir = new BinaryReader(str);
image = bir.ReadBytes((Int32)str.Length);
byte[] logo;
Stream s = fuUpload.PostedFile.InputStream;
BinaryReader br = new BinaryReader(s);
logo = br.ReadBytes((Int32)s.Length);
string sqlStatement = "INSERT INTO InvoiceTable (Uid,email,signature,logo,CreatedDate) VALUES (@Uid,@email,@signature,@logo,@CreatedDate)";
con.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, con);
cmd.Parameters.AddWithValue("@Uid", labelid.Text.ToString());
cmd.Parameters.AddWithValue("@email", user.Text.ToString());
cmd.Parameters.AddWithValue("@signature", image);
cmd.Parameters.AddWithValue("@logo", logo);
cmd.Parameters.AddWithValue("@CreatedDate", DateTime.Now);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
}