2
Answers

connecting C# to database

Ask a question
Problem
I am connecting my C# application to SQL Sever database using web.config settings but getting following error message:

{"Format of the initialization string does not conform to specification starting at index 0."}

My web.config file:

<connectionStrings>
  <add name="DatabaseConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
   providerName="System.Data.SqlClient" />
 </connectionStrings>


My C# file:


protected void Button1_Click(object sender, EventArgs e)
{
  if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{

byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);

SqlConnection myConnection = new SqlConnection("DatabaseConnectionString1");
SqlCommand storeimage = new SqlCommand("INSERT INTO ImageGallery "

+"(Image_Content, Image_Type, Image_Size) "
+" values (@image, @imagetype, @imagesize)", myConnection);
storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;
storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value
= FileUpload1.PostedFile.ContentType;
storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value
= FileUpload1.PostedFile.ContentLength;

myConnection.Open();
storeimage.ExecuteNonQuery();
myConnection.Close();
}
    }




In above coding underlined text has some error?

Please Fix it










Answers (2)