Hello forum,
I have 2 file upload controls (DocFile and LogoFile) where 2 images will be uploaded into database. One of the file upload controls will have its File Extension set to (.png) only, while the other file upload control will accept .png or .jpg/jpeg – The DocFile will accept .jpg, .jpeg or .png, and the LogoFile will only accept .png
How can I set this insert code, so that there will not be any conflict during insertion of data into the database?
Here is my html and insert code.
HTML
<div class="container-fluid">
<label for="txtUsername" style="font-weight: 500;">Name of Individual/Organization</label>
<asp:TextBox ID="usertext" runat="server" CssClass="form-control" Font-Size="11pt" placeholder="Organization or Individual Name" /><br />
<label for="txtUsername" style="font-weight: 500;">Email</label>
<asp:TextBox ID="textmail" runat="server" CssClass="form-control" Font-Size="11pt" placeholder="Email Address" />
<asp:RegularExpressionValidator ID="Regex" runat="server" Font-Size="8pt" ErrorMessage="Please provide a Valid Mail Address" ValidationExpression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" ControlToValidate="mailtxtbx" Display="Dynamic" ForeColor="Red"></asp:RegularExpressionValidator><br />
<label for="txtPassword" style="font-weight: 500;">Password</label>
<div class="input-group">
<asp:TextBox ID="pass" runat="server" TextMode="Password" CssClass="form-control" Font-Size="11pt" placeholder="Password" />
<div class="input-group-append">
<span class="input-group-text" style="background-color: #fff; color: #2d6193; border-bottom: 1px solid #d9dcdd; border-top: 1px solid #d9dcdd;">
<span id="toggle_pw" class="fa fa-fw fa-eye field_icon" style="cursor: pointer;"></span>
</span>
</div>
</div>
<asp:RegularExpressionValidator ID="Regex3" runat="server" Font-Size="8pt" ErrorMessage="Minimum of 8 characters [UpperCase, LowerCase, Number and Special Character]" ValidationExpression="(?=^.{8,}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+}{":;'?/>.<,])(?!.*\s).*$" ControlToValidate="pass" ForeColor="Red"></asp:RegularExpressionValidator><br />
<div class="row" style="margin-top: 3%;">
<div style="margin: 0 auto; text-align: left;">
<p style="font-size: 9pt; font-weight: 600; margin-top: 0%;">Document Upload</p>
<asp:FileUpload ID="DocFile" runat="server" Font-Size="9pt" Width="230px" onchange="ImagePreview(this);" />
</div>
<div style="margin: 0 auto; text-align: left;">
<p style="font-size: 9pt; font-weight: 600; margin-top: 0%;">Logo Upload</p>
<asp:FileUpload ID="LogoFile" runat="server" Width="230px" Font-Size="9pt" />
</div>
</div>
<asp:Button ID="Button2" runat="server" BackColor="#32657c" Text="Register" Class="btn btn-primary" OnClick="Button2_Click" />
<br />
</div>
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True");
using (SqlCommand cmd = new SqlCommand())
{
if (DocFile.PostedFile.FileName != "")
{
string filePath = LogoFile.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = string.Empty;
int size = DocFile.FileBytes.Length;
int imgsize = LogoFile.FileBytes.Length;
//Set the contenttype
if (ext == ".png")
{
contenttype = "image/png";
}
if (contenttype != string.Empty)
{
int Id = -1;
byte[] image;
Stream s = DocFile.PostedFile.InputStream;
BinaryReader br = new BinaryReader(s);
image = br.ReadBytes((Int32)s.Length);
byte[] logoimg;
Stream str = LogoFile.PostedFile.InputStream;
BinaryReader byrdr = new BinaryReader(str);
logoimg = byrdr.ReadBytes((Int32)s.Length);
string query = @"INSERT INTO NewCustomersTable (email, pass, Username, image, img_Logo) VALUES (@email, @pass, @Username, @image, @img_Logo);
SELECT SCOPE_IDENTITY();";
using (SqlCommand objCMD = new SqlCommand(query, con))
{
objCMD.Parameters.Add("@mail", SqlDbType.VarChar, 50).Value = textmail.Text.Trim();
objCMD.Parameters.Add("@pass", SqlDbType.VarChar, 100).Value = pass.Text.Trim();
objCMD.Parameters.Add("@Userame", SqlDbType.VarChar, 50).Value = usertext.Text.Trim();
objCMD.Parameters.Add("@image", SqlDbType.VarBinary).Value = image;
objCMD.Parameters.Add("@img_Logo", SqlDbType.VarBinary).Value = logoimg;
cmd.Connection = con;
con.Open();
object returnObj = objCMD.ExecuteScalar();
if (returnObj != null)
{
int.TryParse(returnObj.ToString(), out Id);
}
con.Close();
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Unrecognized File format" + "JPG/PNG formats only');", true);
}
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Upload Document File');", true);
}
}
}