Click here to Skip to main content
15,913,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
//code to save to database  
 private void btnSave_Click(object sender, EventArgs e)
        {
            byte[] imageBt = null;
            FileStream fstream = new FileStream(this.txtImgPath.Text,FileMode.Open,FileAccess.Read);
            BinaryReader Br = new BinaryReader(fstream);
            imageBt = Br.ReadBytes((int)fstream.Length);
           // byte[] pic = stream.ToArray();
            try
            {
                conDB.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = conDB;
                command.CommandText = "insert into abaanaCC (CCSpn_CODE,CCFname,CCLname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCImage)" +
                    " values ('" + spn_codetxt.Text + "','" + txtfname.Text + "','" + lnametxt.Text + "','" + mnametxt.Text + "','" + DOBDTPicker1.Text + "','" + gendercomboBox.Text + "','" + schtxt.Text + "','" + classcomboBox.Text + "','" + villatxt.Text + "','" + siblingscombobx.Text + "','" + guardiantxt.Text + "','" + contacttxt.Text + "',@IMG) ";
                command.Parameters.Add(new OleDbParameter("@IMG",imageBt));
                //command.Parameters.AddWithValue("@IMG",pic);
                command.ExecuteNonQuery();
                MessageBox.Show("Record Saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to save" + ex);
            }
            conDB.Close();
        }

//data repeater code. And i am getting error: there is no row at position 14
 private void Update_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'abaanaDataSet.abaanaCC' table. You can move, or remove it, as needed.
            this.abaanaCCTableAdapter.Fill(this.abaanaDataSet.abaanaCC);
            int c = this.abaanaDataSet.abaanaCC.Rows.Count;
            if (c > 0) 
            {
                byte[] mydata = new byte[0];
                mydata = (byte[])(this.abaanaDataSet.abaanaCC.Rows[14]["CCImage"]);//error here!!!
            //   mydata  = (byte[])(this.abaanaDataSet.abaanaCC.Rows[c]);
                MemoryStream stream = new MemoryStream(mydata);
                cCImagePictureBox.Image = Image.FromStream(stream);

            }
Posted
Updated 21-Sep-15 2:25am
v4
Comments
aarif moh shaikh 21-Sep-15 5:46am    
is this inserted record in db?
and where image showing code??
Ayeeta 21-Sep-15 6:19am    
yes this inserts into the DB, image column appears as 'long binary data' in the database

Naveen.Sanagasetti 21-Sep-15 6:10am    
Could you please confirm, whether image gets saved in your database or not.?
If Yes, then please post your getting data method.
Ayeeta 21-Sep-15 6:23am    
yes saving to database is fine, retrieving to data repeater is fine except for the image, picture box is empty, i haven't written any getting data method

the commented "//data repeater code" at the bottom is all i have
Ayeeta 21-Sep-15 8:26am    
i am now getting the error: there is no row at position 14

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