更新图片datagrid C# [英] Update picture datagrid C#

查看:67
本文介绍了更新图片datagrid C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Insert picture working fine, I have problem with update...some help?





错误是(更新时)



Error is(when update)

Object reference not set to an instance of an object





我尝试过的:





What I have tried:

string fileName;  







private void buttonupload_Click(object sender, EventArgs e)  
       {  
           //Read image file  
           using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "JPEG|*.jpg|BMP|*.bmp|PNG|*.png|All Files|*.*", ValidateNames = true, Multiselect = false })  
           {  
               if (ofd.ShowDialog() == DialogResult.OK)  
               {  
                   fileName = ofd.FileName;  
  
                   pictureBox1.Image = System.Drawing.Image.FromFile(fileName);  
               }  
           }  
       }  







private void buttonsave_Click(object sender, EventArgs e)  
        {  
  
            byte[] img_arr = null;  
            MemoryStream ms = new MemoryStream();  
            pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);  
            img_arr = ms.GetBuffer();  
  
 using (SqlConnection openCon = new SqlConnection(cs))  
                {  
                    string saveStaff = "INSERT INTO dbo.radnici ( data) VALUES (@data)";  
  
                    using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
                    {  
                     querySaveStaff.Connection = openCon;  
                     querySaveStaff.Parameters.AddWithValue("@data", img_arr);  
  
  
                            openCon.Open();  
                        querySaveStaff.ExecuteNonQuery();  
                        openCon.Close();  
  
                    }  
 }  
  
 else  
            {  
  
                using (SqlConnection openCon = new SqlConnection(cs))  
                {  
                   string updateStaff = "UPDATE dbo.radnici SET data=@data  WHERE id= " + idTextBox.Text;  
  
                    using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))  
                    {  
                        queryupdateStaff.Connection = openCon;                            
                            queryupdateStaff.Parameters.AddWithValue("@data", img_arr);  
  
  
                            openCon.Open();  
                        queryupdateStaff.ExecuteNonQuery();  
                        MessageBox.Show("Uspješno ste izmenili stavku!", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);  
                        openCon.Close();  
  
                    }  
  
                }  
  
            }      

推荐答案

using (SqlCommand querySaveStaff = new SqlCommand(saveStaff))





此行我应该使用



this line i this you should use

updateStaff

更新时

inserted of

saveStaff

更新时的查询参数

in query parameter while updating


这篇关于更新图片datagrid C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆