Database is .mdf, datatype of column is varchar << must be used as project requirement
I input textFilePath.text that I generate from txtFilePath.Text = open.FileName;
, but it makes an error when I press button input. The error that is displayed on the line cmd.CommandText
is
Sqlexception is unhandled : String or binary data would be truncated. The statement has been terminated.
But if I replace the textFilePath.text
with other string it will be success to input to the database.
This is the code, thanks for helping me.
private void Save()
{
txtFilePath.Text.ToString();
cn.Open();
cmd.CommandText = "insert into Inventory (ID,Item,Gender,Qty,Price,FilePath) values('" + txtID.Text + "','" + txtItem.Text + "','" + Gender + "','" + numQty.Value + "','" + numPrice.Value + "','" + txtFilePath.Text + "')";
cmd.ExecuteNonQuery();
cmd.Clone();
cn.Close();
}
private void ButtonChoose_Click(object sender, EventArgs e)
{
try
{
// open file dialog
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
// display image in picture box
pictureBox1.Image = new Bitmap(open.FileName);
// image file path
txtFilePath.Text = open.FileName;
}
}
catch (Exception)
{
throw new ApplicationException("Image loading error....");
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (txtID.Text != "" & txtItem.Text != "" & Gender != "" & numPrice.Value > 0 & numQty.Value > 0 & txtFilePath.Text != "")
{
Save();
MessageBox.Show("Recorded");
gridViewLoad();
}
else
{
MessageBox.Show("all field must be filled");
}
}