Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Entry Image to sql server

$
0
0

Hi

this is my code for upload image and save image in folder

HttpFileCollection uploadFilCol = Request.Files;
for (int i = 0; i < uploadFilCol.Count; i++)
{
HttpPostedFile file = uploadFilCol[i];
string fileExt = Path.GetExtension(file.FileName).ToLower();
string fileName = Path.GetFileName(file.FileName);

if (fileName != string.Empty)
{
try
{
if (fileExt == ".jpg" || fileExt == ".jpeg" || fileExt == ".JPEG" || fileExt == ".JPG")
{
//Save to the disk
FileUpload6.SaveAs(Server.MapPath("~/ImageStore/" + fileName));
FileUpload7.SaveAs(Server.MapPath("~/ImageStore/" + fileName));
FileUpload8.SaveAs(Server.MapPath("~/ImageStore/" + fileName));
FileUpload9.SaveAs(Server.MapPath("~/ImageStore/" + fileName));
FileUpload10.SaveAs(Server.MapPath("~/ImageStore/" + fileName));

//Add Entry to DataBase
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["What I must inserted in here?"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "IDQuestion,Number,Pertanyaan,ChoiceA,ChoiceB,ChoiceC,ChoiceD,ChoiceE,IDQuiz,CorrectAnswer)" + " VALUES (@IDQuestion,@Number,@Pertanyaan,@ChoiceA,@ChoiceB,@ChoiceC,@ChoiceD,@ChoiceE,@IDQuiz,@CorrectAnswer)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@FileName", fileName);
cmd.Parameters.AddWithValue("@FilePath", "~/ImageStore/" + fileName);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
}
}

when I run the application, the image only save in folder not in sqlserver

error appear

Object reference not set to an instance of an object.Object reference not set to an instance of an object.Object reference not set to an instance of an object.Object reference not set to an instance of an object.Object reference not set to an instance of an object.

So I want to ask?

1. String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["What I must insert in here?"].ConnectionString; ---> What I must insert in [""]

2. Is my code wrong? If yes, could you help me to make my code correct?


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>