Hi RichardSa,
Check the file upload has file or not. If no file then set the byte as null. The after checking pass the parameter as DBNull if byte is null.
Use below updated code.
HTML
<div class="table-cover" style="max-width: 90%; padding-left: 10%;">
<div class="row">
<div class="col">
Enter Name:<br />
<asp:TextBox ID="txtName" runat="server" />
</div>
<div class="col">
Enter Subject:<br />
<asp:TextBox ID="txtSubject" runat="server" />
</div>
</div>
<br />
<asp:TextBox ID="txtComment" runat="server" Font-Size="9pt" placeholder="Your Posts" TextMode="MultiLine" CssClass="form-control" />
<asp:FileUpload ID="postimgupload" runat="server" Style="display: none" />
<img id="imgFileUpload" alt="" runat="server" src="~/images/sidenav/camera.png" width="28" height="28" style="cursor: pointer;" />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Post" class="btn btn-primary navbar-btn" OnClick="Button1_Click1" />
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var fileUpload = $("[id*=postimgupload]");
var img = $("[id*=imgFileUpload]");
img.click(function () { fileUpload.click(); });
fileUpload.change(function () {
var reader = new FileReader();
reader.onload = function (e) {
$("[id*=imgFileUpload]").attr("src", e.target.result);
}
reader.readAsDataURL($(this)[0].files[0]);
});
});
</script>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Code
C#
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
protected void Button1_Click1(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtName.Text) & !string.IsNullOrEmpty(txtComment.Text) & !string.IsNullOrEmpty(txtSubject.Text))
{
byte[] image = null;
if (postimgupload.HasFile)
{
Stream s = postimgupload.PostedFile.InputStream;
BinaryReader br = new BinaryReader(s);
image = br.ReadBytes((Int32)s.Length);
}
con.Open();
SqlCommand cmd = new SqlCommand("insert into [Table] (Username,Subject,Comment,PostedDate,imgpost) values(@Username,@Subject,@Comment,@PostedDate,@imgpost)", con);
cmd.Parameters.AddWithValue("@Username", txtName.Text);
cmd.Parameters.AddWithValue("@Subject", txtSubject.Text);
cmd.Parameters.AddWithValue("@Comment", txtComment.Text);
cmd.Parameters.AddWithValue("@PostedDate", DateTime.Now);
if (image == null || image.Length == 0)
{
cmd.Parameters.Add("@imgpost", SqlDbType.VarBinary, -1).Value = DBNull.Value;
}
else
{
cmd.Parameters.AddWithValue("@imgpost", image);
}
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect(Request.Url.AbsoluteUri);
}
else
{
}
}
VB.Net
Private cmd As SqlCommand = New SqlCommand()
Private sda As SqlDataAdapter = New SqlDataAdapter()
Private ds As DataSet = New DataSet()
Private dt As DataTable = New DataTable()
Private con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As EventArgs)
If Not String.IsNullOrEmpty(txtName.Text) And Not String.IsNullOrEmpty(txtComment.Text) And Not String.IsNullOrEmpty(txtSubject.Text) Then
Dim image As Byte() = Nothing
If postimgupload.HasFile Then
Dim s As Stream = postimgupload.PostedFile.InputStream
Dim br As BinaryReader = New BinaryReader(s)
image = br.ReadBytes(CType(s.Length, Int32))
End If
con.Open()
Dim cmd As SqlCommand = New SqlCommand("insert into [Table] (Username,Subject,Comment,PostedDate,imgpost) values(@Username,@Subject,@Comment,@PostedDate,@imgpost)", con)
cmd.Parameters.AddWithValue("@Username", txtName.Text)
cmd.Parameters.AddWithValue("@Subject", txtSubject.Text)
cmd.Parameters.AddWithValue("@Comment", txtComment.Text)
cmd.Parameters.AddWithValue("@PostedDate", DateTime.Now)
If image Is Nothing OrElse image.Length = 0 Then
cmd.Parameters.Add("@imgpost", SqlDbType.VarBinary, -1).Value = DBNull.Value
Else
cmd.Parameters.AddWithValue("@imgpost", image)
End If
cmd.ExecuteNonQuery()
con.Close()
Response.Redirect(Request.Url.AbsoluteUri)
Else
End If
End Sub