Click here to Skip to main content
16,014,392 members
Home / Discussions / C#
   

C#

 
AnswerRe: Store multiple checkbox values in a single field in database Pin
JollyMansArt5-Aug-09 21:14
JollyMansArt5-Aug-09 21:14 
GeneralRe: Store multiple checkbox values in a single field in database Pin
Greg Chelstowski5-Aug-09 21:19
Greg Chelstowski5-Aug-09 21:19 
GeneralRe: Store multiple checkbox values in a single field in database Pin
JollyMansArt5-Aug-09 21:23
JollyMansArt5-Aug-09 21:23 
GeneralRe: Store multiple checkbox values in a single field in database Pin
Mycroft Holmes5-Aug-09 21:20
professionalMycroft Holmes5-Aug-09 21:20 
AnswerRe: Store multiple checkbox values in a single field in database Pin
Greg Chelstowski5-Aug-09 21:18
Greg Chelstowski5-Aug-09 21:18 
AnswerRe: Store multiple checkbox values in a single field in database Pin
Adam Jasper5-Aug-09 23:49
Adam Jasper5-Aug-09 23:49 
GeneralRe: Store multiple checkbox values in a single field in database Pin
elci6-Aug-09 0:12
elci6-Aug-09 0:12 
QuestionNeed help retreiving the image from the database... Pin
JollyMansArt5-Aug-09 20:53
JollyMansArt5-Aug-09 20:53 
I am able to store my pictures into the sql server using the following code:

private void pictureBox3_DoubleClick(object sender, EventArgs e)
                  {
                        intASUserListSelectedIndex = lbxAssistSuiteUserList.SelectedIndex;
                        bool FoundUser = false;
                        FileDialog fldlg = new OpenFileDialog();
                        //specify your own initial directory
                       
                        if (lbxAssistSuiteUserList.SelectedIndex > -1)
                        {
                              try
                              {
                                    fldlg.InitialDirectory = @":C\";
                                    //this will allow only those\ file extensions to be added
                                    fldlg.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif|(*.gif)|*.gif|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg|(*.bmp)|*.bmp|(*.wmf)|*.wmf|(*.png)|*.png";
                                    //fldlg.ShowDialog();
                                    if (fldlg.ShowDialog() == DialogResult.OK)
                                    {
                                          imagename   = fldlg.FileName;
                                          Bitmap newimg = new Bitmap(imagename);
                                          pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
                                          pictureBox3.Image =(Image)newimg;
                                          //pictureBox3.Image = System.Drawing.Image.FromFile(openFileDialog1.FileName);
                                          pictureBox3.Refresh();

                                          //Update the SQL Tables.
                                          lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
                                          btnUpdateAssistUsers_Click(sender, e);
                                          lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
                                          updatedata();
                                    }
                                   
                                   
                                    fldlg = null;
                                    lbxAssistSuiteUserList.SelectedIndex = intASUserListSelectedIndex;
                              }
                              catch (System.ArgumentException ae)
                              {
                                    imagename = " ";
                                    MessageBox.Show(ae.Message.ToString());
                              }
                              catch (Exception ex)
                              {
                                    MessageBox.Show(ex.Message.ToString());
                              }
                        }
                  }

            private void updatedata()
            {

                  bool FoundMe = false;
                  int UserIndex = -1;
            //use filestream object to read the image.
            //read to the full length of image to a byte array.
            //add this byte as an oracle parameter and insert it into database.

                  try
                  {
                        //proceed only when the image has a valid path
                        if (imagename != "")
                        {
                        FileStream fs;
                        fs = new FileStream(@imagename, FileMode.Open, FileAccess.Read);

                        //a byte array to read the image
                        byte[] picbyte = new byte[fs.Length];
                        fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
                        fs.Close();

                        //open the database using odp.net and insert the data
                        string connstr = WhatIsMyConnectionString;
                        SqlConnection conn = new SqlConnection(connstr);
                        conn.Open();

                        //Find the index
                        for (i = 0; i < ApplicationUsersArray.Count; i++)
                        {
                              if (((DomainUsersClass)ApplicationUsersArray[i]).WindowsLogonName == lbxAssistSuiteUserList.Items[lbxAssistSuiteUserList.SelectedIndex].ToString())
                              {
                                    FoundMe = true;
                                    break;
                              }
                        }
                        if (FoundMe = true)
                        {
                              UserIndex = Convert.ToInt32(((DomainUsersClass)ApplicationUsersArray[i]).Index);

                        }
                        else
                        {
                              MessageBox.Show("We have a serious problem. This should Never Happen. \n The Array did not find the correct users to grab their index for the Picture. \n The Application will now quit.");
                              Application.Exit();
                              Application.ExitThread();
                        }

                        string query;
                        query = "insert into tblApplicationUsersPic(id_image,pic) values(" +
                        UserIndex.ToString() + "," + " @pic)";  

                        SqlParameter picparameter = new SqlParameter();
                        picparameter.SqlDbType = SqlDbType.Image;
                        picparameter.ParameterName = "pic";
                        picparameter.Value = picbyte;
                        SqlCommand cmd = new SqlCommand(query, conn);
                        cmd.Parameters.Add(picparameter);
                        cmd.ExecuteNonQuery();

                        if (FoundMe = true)
                        {
                              SQLUpdate(WhatIsMyConnectionString, "tblApplicationUsers", "tblApplicationUsersPic_idx = (Select idx_tblApplicationUserPic from tblApplicationUsersPic where id_image = '" + UserIndex.ToString() + "') ","delflag = 0 and idx_tblApplicationUsers = '" + UserIndex.ToString() + "' ");
                              //UserIndex = Convert.ToInt32(((DomainUsersClass)ApplicationUsersArray[i]).Index);

                        }
                        MessageBox.Show("Image Added");
                        cmd.Dispose();
                        conn.Close();
                        conn.Dispose();
                        Connection();

                        }


                  }
                  catch (Exception ex)
                  {
                        MessageBox.Show(ex.Message);
                  }

                       

            }

            private void Connection()
            {
                  //connect to the database and table
                  //selecting all the columns
                  //adding the name column alone to the combobox

                  try
                  {
                        string connstr = WhatIsMyConnectionString;
                        SqlConnection conn = new SqlConnection(connstr);
                        conn.Open();
                        empadap1 = new SqlDataAdapter();

                        empadap1.SelectCommand = new SqlCommand("SELECT * FROM tblApplicationUsersPic", conn);
                        dset = new DataSet("dset");
                        empadap1.Fill(dset);
                        DataTable dtable;

                        dtable = dset.Tables[0];
                        comboBox1.Items.Clear();
                        foreach (DataRow drow in dtable.Rows)
                        {
                              comboBox1.Items.Add(drow[0].ToString());
                              comboBox1.SelectedIndex = 0;
                        }

                  }
                  catch (Exception ex)
                  {
                        MessageBox.Show(ex.Message);
                  }
            }






But I am unable to retreive the picture from the Sql Server.
Here is my current code to retreive the picture, it keeps erroring saying I do not have something defined and when I define it it still does not work....

Here is the code I use to retreive the image from the database.
//Picture of the User
                              DataTable dataTable = dset.Tables[0];
                              if (pictureBox3.Image != null)
                              {
                                    pictureBox3.Image.Dispose();
                              }
                             
                              for (i = 0; i < ApplicationUsersArray.Count; i++)
                              {
                                    if (((DomainUsersClass)ApplicationUsersArray[i]).WindowsLogonName == lbxAssistSuiteUserList.Items[lbxAssistSuiteUserList.SelectedIndex])
                                    {
                                          break;
                                    }
                              }
                              if (((DomainUsersClass)ApplicationUsersArray[i]).PicIndex != "0" || ((DomainUsersClass)ApplicationUsersArray[i]).PicIndex != "")
                              {
                                    try
                                    {
                                    //using filestream object write the column as bytes and store it as an image
                                          FileStream FS1 = new FileStream("image.jpg", FileMode.Create);
                                          foreach (DataRow dataRow in dataTable.Rows)
                                          {
                                                //if (dataRow[0].ToString() ==
                                                byte[] blob = (byte[])dataRow[1];
                                                FS1.Write(blob, 0, blob.Length);
                                                FS1.Close();
                                                FS1 = null;
                                                pictureBox3.Image = Image.FromFile("image.jpg");
                                                pictureBox3.SizeMode = PictureBoxSizeMode.Zoom;
                                                pictureBox3.Refresh();
                                          }
                                    }
                                    catch
                                    {
                                          MessageBox.Show("Picture retrival Error");
                                          ErrorLogWriter("We have a issue with retreiving the picture stored in the sql server table.");
                                    }
                              }
AnswerRe: Need help retreiving the image from the database... Pin
Henry Minute6-Aug-09 2:46
Henry Minute6-Aug-09 2:46 
QuestionCrystal Report Formula Pin
mjawadkhatri5-Aug-09 19:25
mjawadkhatri5-Aug-09 19:25 
AnswerRe: Crystal Report Formula Pin
JollyMansArt5-Aug-09 19:36
JollyMansArt5-Aug-09 19:36 
GeneralRe: Crystal Report Formula Pin
mjawadkhatri5-Aug-09 22:23
mjawadkhatri5-Aug-09 22:23 
GeneralVoice file conversion Pin
Pandu4385-Aug-09 18:28
Pandu4385-Aug-09 18:28 
QuestionDelimeters to split strings Pin
gamer11275-Aug-09 18:19
gamer11275-Aug-09 18:19 
AnswerRe: Delimeters to split strings Pin
dan!sh 5-Aug-09 18:27
professional dan!sh 5-Aug-09 18:27 
GeneralRe: Delimeters to split strings Pin
gamer11276-Aug-09 0:11
gamer11276-Aug-09 0:11 
AnswerRe: Delimeters to split strings Pin
Luc Pattyn5-Aug-09 23:50
sitebuilderLuc Pattyn5-Aug-09 23:50 
GeneralRe: Delimeters to split strings Pin
gamer11276-Aug-09 0:47
gamer11276-Aug-09 0:47 
GeneralRe: Delimeters to split strings Pin
Luc Pattyn6-Aug-09 0:53
sitebuilderLuc Pattyn6-Aug-09 0:53 
GeneralRe: Delimeters to split strings Pin
gamer11276-Aug-09 1:07
gamer11276-Aug-09 1:07 
AnswerRe: Delimeters to split strings Pin
Luc Pattyn6-Aug-09 1:14
sitebuilderLuc Pattyn6-Aug-09 1:14 
GeneralRe: Delimeters to split strings Pin
gamer11276-Aug-09 1:31
gamer11276-Aug-09 1:31 
GeneralRe: Delimeters to split strings Pin
Luc Pattyn6-Aug-09 1:42
sitebuilderLuc Pattyn6-Aug-09 1:42 
GeneralRe: Delimeters to split strings Pin
gamer11276-Aug-09 4:15
gamer11276-Aug-09 4:15 
GeneralRe: Delimeters to split strings Pin
Luc Pattyn6-Aug-09 4:32
sitebuilderLuc Pattyn6-Aug-09 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.