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.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Data.Odbc;
- using System.IO;
-
- namespace csharp_report
- {
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- }
-
- OdbcConnection CONN;
- OdbcCommand CMD;
- DataSet DS;
- OdbcDataAdapter DA;
- OdbcDataReader DR;
- String LokasiData;
-
- public void Koneksi()
- {
- LokasiData = "Driver={MySQL ODBC 5.3 ANSI Driver};database=visualbasic_report;server=localhost;uid=root;pwd=";
- CONN = new OdbcConnection(LokasiData);
- if (CONN.State == ConnectionState.Closed)
- {
- CONN.Open();
- }
- }
-
- public void TampilGrid()
- {
- Koneksi();
- OdbcDataAdapter DA = new OdbcDataAdapter("select * from report", CONN);
- DataSet DS = new DataSet();
- DA.Fill(DS, "report");
- DataGridView1.DataSource = DS.Tables["report"];
- DataGridView1.ReadOnly = true;
- }
-
- public void TampilKembali()
- {
- try
- {
- CONN.Open();
- CMD.Connection = CONN;
- CMD.CommandType = CommandType.Text;
- CMD.CommandText = "Select * from report where kode='" + txtKode.Text + "'";
- DR = CMD.ExecuteReader();
- if (DR.HasRows)
- {
- DR.Read();
- txtNama.Text = DR["nama"].ToString();
- txtAlamat.Text = DR["alamat"].ToString();
- txtPekerjaan.Text = DR["pekerjaan"].ToString();
- byte[] data = (byte[])DR["foto"];
- MemoryStream ms = new MemoryStream(data);
- ptbFoto.BackgroundImage = Image.FromStream(ms);
- ptbFoto.BackgroundImageLayout = ImageLayout.Stretch;
- DR.Close();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
-
- }
- }
-
- private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- DataGridViewRow row;
- row = DataGridView1.CurrentRow;
- txtKode.Text = row.Cells["kode"].Value.ToString();
- CONN.Close();
- TampilKembali();
- }
-
- private void frmMain_Load(object sender, EventArgs e)
- {
- TampilGrid();
- }
-
- private void btnReport_Click(object sender, EventArgs e)
- {
- String sTitle;
- sTitle = txtKode.Text;
- View frm = new View(sTitle);
- frm.Show();
- }
- }
- }