I recently learned how to set background image of div based on ImageButton click. I want to learn how to insert this background image that was set by clicking on ImageButton, into database.
Is this how it can be done in the C# below?
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div class="row" runat="server" style="width: 100%; margin: 0 auto; padding: 10px; margin-bottom: 0%;">
<div class="col-sm-3" style="margin-top: 2%; left: 0; z-index: 999; width: 100%; margin: 0 auto; padding: 10px; border-radius: 4px; border: 0.2px solid #f0f1f2; background-color: #fff;">
<div class="card" style="width: auto; background-color: #fff; border: none;">
<div class="card-body">
<asp:DataList ID="TemplateList" runat="server" Width="100%" RepeatDirection="Horizontal" RepeatColumns="3" CellSpacing="2" RepeatLayout="Table" CssClass="row">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" CssClass="ImageButton" runat="server" ImageUrl='<%#Eval("Url")%>' Height="50" BorderStyle="Solid" BorderWidth="1" />
</ItemTemplate>
</asp:DataList>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="card1" id="IDcard" runat="server" style="width: 100%; border-radius: 6px; border: 2px solid #f0f1f2; background-color: #fff;">
<div class="row" style="width: 100%; margin: 0 auto; padding: 6px; margin-top: 3%;">
<div class="col-2">
<asp:Image ID="Image2" ImageUrl="image.png" AlternateText="logo" runat="server" Width="35" Height="30" />
</div>
<div class="col-8">
<span style="float: inline-start;">
<div style="text-align: center;">
<asp:Label ID="lblName" runat="server" Text="Organization Name"></asp:Label>
</div>
<div style="text-align: center;">
<asp:Label ID="addresslbl" runat="server" Text="Contact Address"></asp:Label>
</div>
</span>
</div>
</div>
<div class="contained" style="margin-bottom: 3%; margin: 0 auto; text-align: center;">
<br />
<div style="text-align: center; margin-top: 10%;">
<div style="text-align: center; margin-top: 10%;">
<asp:Image ID="Image1" ImageUrl="Test.png" runat="server" Width="80" Height="80" />
</div>
<div style="text-align: center; margin-top: 3%;">
<asp:Label ID="Namelbl" runat="server" Text="Holder's Name"></asp:Label>
</div>
<div style="text-align: center; margin-top: 3%;">
<asp:Label ID="Label4" runat="server" Text="Title/Designation/Department"></asp:Label>
</div>
</div>
</div>
<div class="row" style="width: 100%; margin: 0 auto; margin-top: 10%;">
<div class="col-sm-5">
<div class="form-group" style="float: left; font-size: 5pt;">
<div class="input-group">
<asp:Label ID="permitID" runat="server" Text=""></asp:Label>
</div>
</div>
</div>
<div class="col-sm-7">
<div class="form-group" style="float: right; font-size: 5pt;">
<div class="input-group">
<asp:Image ID="Image3" ImageUrl="Test.png" runat="server" Width="80" Height="80" />
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-3" style="margin-top: 2%; left: 0; z-index: 999; width: 100%; margin: 0 auto; padding: 10px; border-radius: 4px; border: 0.2px solid #f0f1f2; background-color: #fff;">
<div class="" style="width: 100%; margin: 0 auto; padding: 6px; margin-top: 1%; border-radius: 4px;">
<div style="margin-top: 1%; margin-bottom: 4%;">
<label style="font-weight: 500; font-size: 8pt; color: #93979e;">Edit your permit</label>
</div>
<asp:Label ID="headcolor" runat="server">Header Color</asp:Label>
<div class="input-group" style="font-size: 4pt;">
<asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="None" Font-Size="4pt" CssClass="form-control" />
<cc1:ColorPickerExtender ID="ColorPicker1" runat="server" TargetControlID="TextBox1"
PopupButtonID="ColorBtn" PopupPosition="TopRight" OnClientColorSelectionChanged="ColorChanged" />
<div class="input-group-append">
<span class="input-group-text" id="ColorBtn" runat="server" style="height: auto; background-color: transparent; color: #404344;">
<span id="toggle_picker" class="fad fa-eye-dropper" aria-hidden="true" style="cursor: pointer; font-size: 11pt; border: none;"></span>
</span>
</div>
</div>
<asp:Label ID="Label3" runat="server">Label Color</asp:Label>
<div class="input-group" style="font-size: 4pt;">
<asp:TextBox ID="BackTxt" runat="server" AutoCompleteType="None" Font-Size="4pt" CssClass="form-control" />
<cc1:ColorPickerExtender ID="ColorPickerExtender1" runat="server" TargetControlID="BackTxt"
PopupButtonID="BackColorBtn" PopupPosition="TopRight" OnClientColorSelectionChanged="ChangedAllColor" />
<div class="input-group-append">
<span class="input-group-text" id="BackColorBtn" runat="server" style="height: auto; background-color: transparent; color: #404344;">
<span id="perm_id" class="fad fa-eye-dropper" aria-hidden="true" style="cursor: pointer; font-size: 11pt; border: none;"></span>
</span>
</div>
</div>
<asp:Label ID="fontlbl" runat="server">Choose Font</asp:Label>
<div class="input-group">
<input type="text" class="fonts" />
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('.ImageButton').click(function () {
var image = $(this).attr('src');
$('#IDcard').css({ 'background-image': 'url(' + image + ')', 'background-repeat': 'no-repeat', 'background-size': 'cover' });
return false;
});
});
</script>
C#
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
byte[] image = Convert.FromBase64String(IDcard.backgorund-image.ImageUrl.Replace("data:image/jpeg;base64,", ""));
byte[] picture = Convert.FromBase64String(Image1.ImageUrl.Replace("data:image/jpeg;base64,", ""));
string sqlStatement = "INSERT INTO InvoiceTable (Name, address, Organization, DateCreated, Department, logo, photo) VALUES (@Name, @address, @Organization, @DateCreated, @Department, @logo, @photo)";
con.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, con);
cmd.Parameters.AddWithValue("@Name", Namelbl.Text.ToString());
cmd.Parameters.AddWithValue("@address", addresslbl.Text.ToString());
cmd.Parameters.AddWithValue("@Organization", lblName.Text.TString());
cmd.Parameters.AddWithValue("@DateCreated", DateTime.UtcNow);
cmd.Parameters.AddWithValue("@Department", Label4.Text.ToString());
cmd.Parameters.AddWithValue("@logo", image);
cmd.Parameters.AddWithValue("@photo", picture);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
}