0
Trying to record in a sql table an image, its name and description and some foreign keys to allow me to search later.
On execution I get error of string conversion for Int 32. But all fields in the table are correctly defined. What am I doing wrong?
private void BotaoGravar_Click(object sender, EventArgs e)
{
string sql = "select * from Banco_Imagens.tb_imagem";
MemoryStream memory = new MemoryStream();
// Verificar se alguma imagem foi seleccionada
if (bmp == null)
{
MessageBox.Show("Por favor seleccione uma imagem, antes de gravar o registo.");
}
else
{
bmp.Save(memory, ImageFormat.Bmp);
}
byte[] foto = memory.ToArray();
SqlCommand comando = new SqlCommand("insert into tb_imagem (imagem,nome_imagem,descr_imagem,iddiag,idlesao,idlesao1,idlesao2,idlesao3,idlesao4,ND) values (@imagem,@nome_imagem,@descr_imagem,@iddiag,@idlesao,@idlesao1,@idlesao2,@idlesao3,@idlesao4,@ND)", con);
SqlCommand cmdInteiro = new SqlCommand(sql, con);
SqlParameter imagem = new SqlParameter("@imagem", SqlDbType.VarBinary);
SqlParameter nome_imagem = new SqlParameter("@nome_imagem", SqlDbType.VarChar);
SqlParameter descr_imagem = new SqlParameter("@descr_imagem", SqlDbType.VarChar);
SqlParameter ND = new SqlParameter("@ND", SqlDbType.Int);
SqlParameter iddiag = new SqlParameter("@iddiag", SqlDbType.Int);
SqlParameter idlesao = new SqlParameter("@idlesao", SqlDbType.Int);
SqlParameter idlesao1 = new SqlParameter("@idlesao1", SqlDbType.Int);
SqlParameter idlesao2 = new SqlParameter("@idlesao2", SqlDbType.Int);
SqlParameter idlesao3 = new SqlParameter("@idlesao3", SqlDbType.Int);
SqlParameter idlesao4 = new SqlParameter("@idlesao4", SqlDbType.Int);
cmdInteiro.CommandType = CommandType.StoredProcedure;
comando.Parameters.AddWithValue("@imagem", SqlDbType.VarBinary);
comando.Parameters["@imagem"].Value = foto;
comando.Parameters.AddWithValue("@nome_imagem", SqlDbType.VarChar);
comando.Parameters["@nome_imagem"].Value = TextBoxNomeImagem.Text;
comando.Parameters.AddWithValue("@descr_imagem", SqlDbType.VarChar);
comando.Parameters["@descr_imagem"].Value = TextBoxDescrImagem.Text;
comando.Parameters.AddWithValue ("@iddiag", SqlDbType.Int);
comando.Parameters["@iddiag"].Value = TextBoxiddiag.Text;
comando.Parameters.AddWithValue ("@idlesao", SqlDbType.Int);
comando.Parameters["@idlesao"].Value = TextBoxidlesao.Text;
comando.Parameters.AddWithValue ("@idlesao1", SqlDbType.Int);
comando.Parameters["@idlesao1"].Value = TextBoxidlesao1.Text;
comando.Parameters.AddWithValue ("@idlesao2", SqlDbType.Int);
comando.Parameters["@idlesao2"].Value = TextBoxidlesao2.Text;
comando.Parameters.AddWithValue ("@idlesao3", SqlDbType.Int);
comando.Parameters["@idlesao3"].Value = TextBoxidlesao3.Text;
comando.Parameters.AddWithValue ("@idlesao4", SqlDbType.Int);
comando.Parameters["@idlesao4"].Value = TextBoxidlesao4.Text;
cmdInteiro.Parameters.AddWithValue("@ND", SqlDbType.Int);
cmdInteiro.Parameters["@ND"].Value = TextBoxND.Text;
//comando.Parameters.AddWithValue ("@idtipolesao", SqlDbType.Int);
//cmd.Parameters["@idtipolesao"].Value = TextBoxidtipolesao;
imagem.Value = foto;
nome_imagem.Value = TextBoxNomeImagem.Text;
descr_imagem.Value = TextBoxDescrImagem.Text;
iddiag.Value = (TextBoxiddiag.Text);
idlesao.Value = (TextBoxidlesao.Text);
idlesao1.Value = (TextBoxidlesao1.Text);
idlesao2.Value = (TextBoxidlesao2.Text);
idlesao3.Value = (TextBoxidlesao3.Text);
idlesao4.Value = (TextBoxidlesao4.Text);
ND.Value = (TextBoxND.Text);
//idtipolesao.Value = TextBoxidtipolesao.Text;
// Verificar se o Número de Doente só contém inteiros
if (Regex.IsMatch(TextBoxND.Text, @"^\D*$"))
{
//Show message and clear input.
MessageBox.Show("Por favor introduza só algarismos.");
TextBoxND.Clear();
}
else
{
comando.Parameters.Add (imagem);
comando.Parameters.Add(nome_imagem);
comando.Parameters.Add(descr_imagem);
comando.Parameters.Add(iddiag);
comando.Parameters.Add(idlesao);
comando.Parameters.Add(idlesao1);
comando.Parameters.Add(idlesao2);
comando.Parameters.Add(idlesao3);
comando.Parameters.Add(idlesao4);
comando.Parameters.Add(ND);
//comando.Parameters.Add(idtipolesao);
}
try
{
con.Open();
comando.ExecuteNonQuery();
MessageBox.Show("Imagem Gravada!");
PictureBox1.Image = null;
TextBoxNomeImagem.Text = "";
TextBoxDescrImagem.Text = "";
TextBoxiddiag.Text = "";
TextBoxDiagnostico.Text = "";
TextBoxLesao.Text = "";
TextBoxLesao1.Text = "";
TextBoxLesao2.Text = "";
TextBoxLesao3.Text = "";
TextBoxLesao4.Text = "";
TextBoxND.Text = "";
//TextBoxTipoLesao.Text = "";
//TextBoxiddiag.Text = "";
//TextBoxidlesao.Text = "";
//TextBoxidlesao1.Text = "";
//TextBoxidlesao2.Text = "";
//TextBoxidlesao3.Text = "";
//TextBoxidlesao4.Text = "";
////TextBoxidtipolesao.Text = "";
}
catch (Exception E)
{
MessageBox.Show(E.Message);
}
finally
{
con.Close();
}
}
After the change, you have given the following error: System.Formatexception: 'Input string with incorrect format.'
– Adriano
I’ve been going over your script a little bit, and I think I’m mixing things up. First, you have an Insert command and then you say sqlcommand type is Stored Procedure. Then I see that you are assigning values to the command parameters and last to cmdInteiro. command.Parameters["@idlesao4"]. Value = Textboxidlesao4.Text; cmdInteiro.Parameters.Addwithvalue("@ND", Sqldbtype.Int); cmdInteiro.Parameters["@ND"]. Value = Textboxnd.Text;
– Sandro Silva
Thanks for your time, Sandro.
– Adriano
I am certainly mixing things up otherwise the "thing" would work. Can you enlighten me how I should then code? I have two problems: 1º if I leave any of the fields idle or idle1-4 blank, I get an error in the attempt to write to the SQL table (type error). The second is an error of " The variable name '@image' has already been declared. Variable Names must be Unique Within a query batch or stored Procedure. Must declare the scalar variable "@ND". How should I do?
– Adriano
@Adriano, sorry I didn’t answer before. For idle fields force a fixed value. It probably gives error because the table must have indicated that these fields cannot be null. While the second problem for "Must declare the scalar variable "@ND"" It seems that the problem is here: cmdInteiro.Parameters.Addwithvalue("@ND", Sqldbtype.Int); cmdInteiro.Parameters["@ND"]. Value = Textboxnd.Text; Should be: command.Parameters.Addwithvalue("@ND", Sqldbtype.Int); command.Parameters["@ND"]. Value = Textboxnd.Text; O of the image I’m not seeing anything. Send the latest version of the code.
– Sandro Silva
tb_image table has all Foreign Keys defined as (FK, int, null). I don’t think that’s what it indicates, but I may be wrong.
– Adriano
If the idle field is a FK then that could be the problem. Null is not an accepted value. As I said, try to force a value that exists in these fields and run the code again. Have you modified what I mentioned in the "@ND" field? If possible send the latest version of the code.
– Sandro Silva
Hi Sandro. I posted the code. You got to see it?
– Adriano