I am trying to insert an image into database with its properties and a couple of textboxes. Here is the HTML code:
<span class="label"><label for="FirstName">First Name: </label></span>
<asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
</div>
<div class="row">
<span class="label"><label for="Surname">Surname: </label></span>
<asp:TextBox ID="Surname" runat="server"></asp:TextBox>
</div>
<div class="row">
<span class="label"><label for="PhotoUpload">Photo: </label></span>
<asp:FileUpload ID="PhotoUpload" runat="server" />
</div>
<div class="row">
<span class="label"> </span>
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
The code-behind:
protected void Button1_Click(object sender, EventArgs e)
{
if (PhotoUpload.HasFile)
{
Stream photoStream = PhotoUpload.PostedFile.InputStream;
int photoLength = PhotoUpload.PostedFile.ContentLength;
string photoMime = PhotoUpload.PostedFile.ContentType;
string photoName = Path.GetFileName(PhotoUpload.PostedFile.FileName);
byte[] photoData = new byte[photoLength - 1];
photoStream.Read(photoData, 0, photoLength);
string CS = ConfigurationManager.ConnectionStrings["mm"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("spLogo", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FName", FirstName.Text);
cmd.Parameters.AddWithValue("@SName", Surname.Text);
cmd.Parameters.AddWithValue("@PData", photoData);
cmd.Parameters.AddWithValue("@PName", photoName);
cmd.Parameters.AddWithValue("@PLength", photoLength);
cmd.Parameters.AddWithValue("@PMime", photoMime);
cmd.ExecuteNonQuery();
}
}
What could be the problem here?
This is my stored procedure:
alter procedure spLogo
@FName nvarchar(50),
@SName nvarchar(50),
@PData VarBinary(max),
@PName nvarchar(50),
@PLength int,
@PMime nvarchar(50)
as
begin
Insert into Employee2 values(@FName,@SName,@PData,@PName,@PLength,@PMime)
END
I am working on an image for the first time so I'm not sure which datatype would be the best but this is the best code I can come up with.