Click here to Skip to main content
15,896,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void Button1_Click(object sender, EventArgs e)
{


if (!FileUpload1.HasFile)
{
Label3.Visible = true;
Label3.Text = "Please Select Image File"; //checking if file uploader has no file selected


}
else
{

FileUpload1.SaveAs(Server.MapPath(Request.ApplicationPath + "//Picture//" //foldername
+ FileUpload1.FileName));
this.Session["Path"] = "~//Picture//" + this.FileUpload1.FileName;
//string path= "~/Picture/" ;

//int length = FileUpload1.PostedFile.ContentLength;
//byte[] pic = new byte[length];


//FileUpload1.PostedFile.InputStream.Read(pic, 0, length);

try
{
cn.Open(); //calling connection method//inserting uploaded image query
SqlCommand cmd = new SqlCommand("insert into tblStudent "
+ "(std_ID,std_Name,std_Password,std_ContactNumber,std_Address,std_Image,module_ID,intake_Code) values (@std_ID,@std_Name,@std_Password,@std_ContactNumber,@std_Address,@std_Image,@module_ID,@intake_Code)", cn);
cmd.Parameters.AddWithValue("@std_ID", TextBox2.Text);
cmd.Parameters.AddWithValue("@std_Name", TextBox1.Text);
cmd.Parameters.AddWithValue("@std_Password", TextBox3.Text);
cmd.Parameters.AddWithValue("@std_ContactNumber", TextBox4.Text);
cmd.Parameters.AddWithValue("@std_Address", TextBox5.Text);
cmd.Parameters.AddWithValue("@std_Image",FileUpload1.FileName );

string checkboxlist;
for (int i = 0; i < CheckBoxList1.Items.Count; i++) {
if (CheckBoxList1.Items[i].Selected) {
checkboxlist = string.Empty;
checkboxlist = CheckBoxList1.Items[i].Text.ToString();

}
}

string module_id = "";
if (CheckBoxList1.Items[0].Selected == true) {
module_id = CheckBoxList1.Items[0].Value;
}

if (CheckBoxList1.Items[1].Selected == true) {

if (module_id == "")
{
module_id = CheckBoxList1.Items[1].Value;
}
else {
module_id = module_id + "%" + CheckBoxList1.Items[1].Value;
}
}

if (CheckBoxList1.Items[2].Selected == true)
{
if (module_id == "")
{
module_id = CheckBoxList1.Items[2].Value;
}
else
{
module_id = module_id + "%" + CheckBoxList1.Items[2].Value;
}

}

string[] words = module_id.Split('%');


cmd.Parameters.AddWithValue("@module_ID", module_id);
cmd.Parameters.AddWithValue("@intake_Code", DropDownList1.SelectedValue);
cmd.ExecuteNonQuery();

System.Windows.Forms.MessageBox.Show("New Account Register Successfully!");
Response.Redirect("AdminHome.aspx", true);
}
finally
{
cn.Close();
}
}
}
Posted

1 solution

issue is on below line
cmd.Parameters.AddWithValue("@std_Image",FileUpload1.FileName );

you may have specify image or varbinary column in database for std_Image column by you have only provide string path when you set the parameter value. your database column expecting Image or binary array ( content of file not the path) as the value.

check below link and follow the steps and learn how you can handle images with database and web application. with that knowledge you can change your project code. As I remember I have given same link in your previous question but it seems you haven't try it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900