maryam64-69
عضو جدید
سلام من از این کد برای درج عکس استفاده میکنم ولی هنگام درج خطا میده
if (pictureBox1.Image != null)
{
try
{
string strConnection = @"Data Source=SFIX-PC;Initial Catalog=GiahanDarooei;Integrated Security=True";
SqlConnection con1 = new SqlConnection(strConnection);
SqlCommand com1 = new SqlCommand();
com1.Connection = con1;
con1.Open();
com1.CommandText = " INSERT INTO T_giah (picture) VALUES (@picture) ";
com1.Parameters.Add("@picture", SqlDbType.Image);
byte[] imageData = ReadFile(openFileDialog1.FileName.ToString());
com1.Parameters["@picture"].Value = imageData;
com1.ExecuteNonQuery();
con1.Close();
MessageBox.Show("Image saved into database", this.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text);
}
finally
{
}
}
else
{
MessageBox.Show("Please select an image and try again");
}
}
private void btn3_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
}
public byte[] ReadFile(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
کسی میدونه واسه چی این خطا رو میده
cannot insert the value NULL into column ‘GiahName’,table ‘datoo.dbo.giah’;column does not allow nulls.INSERT fails.The ststement has been terminated.
if (pictureBox1.Image != null)
{
try
{
string strConnection = @"Data Source=SFIX-PC;Initial Catalog=GiahanDarooei;Integrated Security=True";
SqlConnection con1 = new SqlConnection(strConnection);
SqlCommand com1 = new SqlCommand();
com1.Connection = con1;
con1.Open();
com1.CommandText = " INSERT INTO T_giah (picture) VALUES (@picture) ";
com1.Parameters.Add("@picture", SqlDbType.Image);
byte[] imageData = ReadFile(openFileDialog1.FileName.ToString());
com1.Parameters["@picture"].Value = imageData;
com1.ExecuteNonQuery();
con1.Close();
MessageBox.Show("Image saved into database", this.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, this.Text);
}
finally
{
}
}
else
{
MessageBox.Show("Please select an image and try again");
}
}
private void btn3_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
}
public byte[] ReadFile(string sPath)
{
byte[] data = null;
FileInfo fInfo = new FileInfo(sPath);
long numBytes = fInfo.Length;
FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fStream);
data = br.ReadBytes((int)numBytes);
return data;
}
کسی میدونه واسه چی این خطا رو میده
cannot insert the value NULL into column ‘GiahName’,table ‘datoo.dbo.giah’;column does not allow nulls.INSERT fails.The ststement has been terminated.