0

I'm developing an admin panel and a web service but how to add an image data into SQL Server database and retrieve it in admin panel in GridView? Everything adds fine, shows fine except the images. I'm using HTML Input (file) control. No errors just image is not displaying but lblTest shows accordingly.

And how to make my Button can ADD and Refresh so user can see the new data instantly.

Thank you!

Here is my C# code:

protected void Page_Load(object sender, EventArgs e)
{           
   showSplash();
}

string connStr = WebConfigurationManager.ConnectionStrings["connection"].ConnectionString;

Int32 fileLength = 0;

protected void ButtonTest_Click(object sender, EventArgs e)
{
            HttpPostedFile uploadFile = FileLogo.PostedFile;
            fileLength = uploadFile.ContentLength;

            if (fileLength == 0)
            {
                string filePath = Server.MapPath(@"\icon\no-photo-icon.jpg");
                string fileName = Path.GetFileName(filePath);

                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                fileLength = (Int32)fs.Length;
                Byte[] fileByteArr = new Byte[fileLength];
                fs.Read(fileByteArr, 0, fileLength);

                SqlConnection conn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand("insert into SPLASH (VersionNumber, SplashLabel,LoginID) values (@VersionNumber,@SplashLabel,@LoginID)", conn);

                cmd.Parameters.AddWithValue("@VersionNumber", txtVnum.Value);
                cmd.Parameters.AddWithValue("@SplashLabel", txtSpLabel.Value);
                cmd.Parameters.AddWithValue("@LoginID", txtYourID.Value);
                cmd.Parameters.AddWithValue("@ImageData", fileByteArr);
                cmd.Parameters.AddWithValue("@ImageContentType", "image/jpg");
                cmd.Parameters.AddWithValue("@ImageSize", fileLength);

                lbltest.Text = "added fine with no file";

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                conn.Dispose();
                cmd.Dispose();

            }
            else
            {
                Byte[] fileByteArray = new Byte[fileLength];
                Stream streamObject = uploadFile.InputStream;
                streamObject.Read(fileByteArray, 0, fileLength);

                SqlConnection conn = new SqlConnection(connStr);
                SqlCommand cmd = new SqlCommand("insert into SPLASH (VersionNumber, SplashLabel,LoginID) values (@VersionNumber,@SplashLabel,@LoginID)", conn);

                cmd.Parameters.AddWithValue("@VersionNumber", txtVnum.Value);
                cmd.Parameters.AddWithValue("@SplashLabel", txtSpLabel.Value);
                cmd.Parameters.AddWithValue("@LoginID", txtYourID.Value);
                cmd.Parameters.AddWithValue("@ImageData", fileByteArray);
                cmd.Parameters.AddWithValue("@ImageContentType", "image/jpg");
                cmd.Parameters.AddWithValue("@ImageSize", fileLength);

                lbltest.Text = "added fine";

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                conn.Dispose();
                cmd.Dispose();
            }
}

private void showSplash()
{
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand("select * from SPLASH", conn);

            conn.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Load(rdr);
            GridViewAddSplash.DataSource = dt;
            GridViewAddSplash.DataBind();
            conn.Close();
            conn.Dispose();
            cmd.Dispose();
}

My other related question links:

Ajax tabContainer:

Community
  • 1
  • 1
user3601310
  • 795
  • 2
  • 11
  • 18
  • I know this is not answering your question but are you realy sure you want to store images in the database ? Why not only store the iamge path and save the image on the HD ? http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – fantastik78 Jul 30 '14 at 08:28
  • Thanks for the suggestion. it may sound stupid but what's HD? I'm new to this geeky programming world.... know nothing basically. – user3601310 Jul 30 '14 at 09:00
  • Sorry HDD. I missed a D :p – fantastik78 Jul 30 '14 at 09:12

0 Answers0