Suganda Suganda

Suganda Suganda

  • NA
  • 24
  • 10.7k

Error C#: Object Reference Not Set To an Instance Object?

Mar 19 2018 5:12 AM
I create code for retrieve data from mysql (string and longblob), but i can't see the errors in my code. 
 
can you help me to fix this? I am using mysql (longblob) and retrieve image to picturebox. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.Data.Odbc;  
  11. using System.IO;  
  12.   
  13. namespace csharp_report  
  14. {  
  15.     public partial class frmMain : Form  
  16.     {  
  17.         public frmMain()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         OdbcConnection CONN;  
  23.         OdbcCommand CMD;  
  24.         DataSet DS;  
  25.         OdbcDataAdapter DA;  
  26.         OdbcDataReader DR;  
  27.         String LokasiData;  
  28.   
  29.         public void Koneksi()  
  30.         {  
  31.             LokasiData = "Driver={MySQL ODBC 5.3 ANSI Driver};database=visualbasic_report;server=localhost;uid=root;pwd=";  
  32.             CONN = new OdbcConnection(LokasiData);  
  33.             if (CONN.State == ConnectionState.Closed)  
  34.             {  
  35.                 CONN.Open();  
  36.             }  
  37.         }  
  38.   
  39.         public void TampilGrid()  
  40.         {  
  41.             Koneksi();  
  42.             OdbcDataAdapter DA = new OdbcDataAdapter("select * from report", CONN);  
  43.             DataSet DS = new DataSet();  
  44.             DA.Fill(DS, "report");  
  45.             DataGridView1.DataSource = DS.Tables["report"];  
  46.             DataGridView1.ReadOnly = true;  
  47.         }  
  48.   
  49.         public void TampilKembali()  
  50.         {  
  51.             try  
  52.             {  
  53.                 CONN.Open();  
  54.                 CMD.Connection = CONN;  
  55.                 CMD.CommandType = CommandType.Text;  
  56.                 CMD.CommandText = "Select * from report where kode='" + txtKode.Text + "'";  
  57.                 DR = CMD.ExecuteReader();  
  58.                 if (DR.HasRows)  
  59.                 {  
  60.                     DR.Read();  
  61.                     txtNama.Text = DR["nama"].ToString();  
  62.                     txtAlamat.Text = DR["alamat"].ToString();  
  63.                     txtPekerjaan.Text = DR["pekerjaan"].ToString();  
  64.                     byte[] data = (byte[])DR["foto"];  
  65.                     MemoryStream ms = new MemoryStream(data);  
  66.                     ptbFoto.BackgroundImage = Image.FromStream(ms);  
  67.                     ptbFoto.BackgroundImageLayout = ImageLayout.Stretch;  
  68.                     DR.Close();  
  69.                 }  
  70.             }  
  71.             catch (Exception ex)  
  72.             {  
  73.                 MessageBox.Show(ex.Message);  
  74.                 //throw;  
  75.             }  
  76.         }  
  77.   
  78.         private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)  
  79.         {  
  80.             DataGridViewRow row;  
  81.             row = DataGridView1.CurrentRow;  
  82.             txtKode.Text = row.Cells["kode"].Value.ToString();  
  83.             CONN.Close();  
  84.             TampilKembali();  
  85.         }  
  86.   
  87.         private void frmMain_Load(object sender, EventArgs e)  
  88.         {  
  89.             TampilGrid();  
  90.         }  
  91.   
  92.         private void btnReport_Click(object sender, EventArgs e)  
  93.         {  
  94.             String sTitle;  
  95.             sTitle = txtKode.Text;  
  96.             View frm = new View(sTitle);  
  97.             frm.Show();  
  98.         }  
  99.     }  
  100. }  
 

Answers (3)