L
low_pointer
Guest
i am getting the operands clash exception. its saying that image is not compatible with int. what m i doing wrong . following is my code when i was trying to insert image into sql DB
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.IO;
using System.Data.SqlClient;
namespace Chat_Chor
{
public partial class Form2 : Form
{
SqlConnection conn;
SqlCommand cmd;
SqlDataReader dr;
byte[] image;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "PNG Files(*.png)|*.png|All Files(*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
string picloc = dlg.FileName.ToString();
textBox1.Text = picloc;
FileInfo fi=new FileInfo(picloc);
image = new byte[fi.Length];
pictureBox1.ImageLocation = picloc;
FileStream fstream = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
int i = fstream.Read(image, 0, Convert.ToInt32(fi.Length));
}
// int id;
// BinaryReader br = new BinaryReader(fstream);
// image = br.ReadBytes((int)fstream.Length);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
// id = Convert.ToInt32(textBox2.Text);
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=image;Integrated Security=True;Pooling=False";
conn.Open();
string s;
s = "insert into images(id,image) values(@id,@image)";
cmd = new SqlCommand(s, conn);
cmd.CommandType = CommandType.Text;
MessageBox.Show("s= " + s);
cmd.Parameters.Add("@id", SqlDbType.Int, 4);
cmd.Parameters.Add("@image", SqlDbType.Image);
cmd.Parameters["@id"].Value =Convert.ToInt32(textBox2.Text);
cmd.Parameters["@image"].Value = image;
/* SqlParameter iimage = new SqlParameter("@image", image);
iimage.Value = image;
cmd.Parameters.Add(iimage);
cmd.Parameters.Add(new SqlParameter("@image",SqlDbType.Image));
cmd.Parameters["@image"].Value = image;
cmd.Parameters.Add("@image");
*/
// dr = cmd.ExecuteReader();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
}
}
}
Continue reading...
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.IO;
using System.Data.SqlClient;
namespace Chat_Chor
{
public partial class Form2 : Form
{
SqlConnection conn;
SqlCommand cmd;
SqlDataReader dr;
byte[] image;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "PNG Files(*.png)|*.png|All Files(*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
string picloc = dlg.FileName.ToString();
textBox1.Text = picloc;
FileInfo fi=new FileInfo(picloc);
image = new byte[fi.Length];
pictureBox1.ImageLocation = picloc;
FileStream fstream = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read);
int i = fstream.Read(image, 0, Convert.ToInt32(fi.Length));
}
// int id;
// BinaryReader br = new BinaryReader(fstream);
// image = br.ReadBytes((int)fstream.Length);
}
private void button2_Click(object sender, EventArgs e)
{
try
{
// id = Convert.ToInt32(textBox2.Text);
conn = new SqlConnection();
conn.ConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=image;Integrated Security=True;Pooling=False";
conn.Open();
string s;
s = "insert into images(id,image) values(@id,@image)";
cmd = new SqlCommand(s, conn);
cmd.CommandType = CommandType.Text;
MessageBox.Show("s= " + s);
cmd.Parameters.Add("@id", SqlDbType.Int, 4);
cmd.Parameters.Add("@image", SqlDbType.Image);
cmd.Parameters["@id"].Value =Convert.ToInt32(textBox2.Text);
cmd.Parameters["@image"].Value = image;
/* SqlParameter iimage = new SqlParameter("@image", image);
iimage.Value = image;
cmd.Parameters.Add(iimage);
cmd.Parameters.Add(new SqlParameter("@image",SqlDbType.Image));
cmd.Parameters["@image"].Value = image;
cmd.Parameters.Add("@image");
*/
// dr = cmd.ExecuteReader();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
}
}
}
Continue reading...