Save multiple photos in SQL Database in same ID

Asked

Viewed 136 times

1

I’m having trouble saving multiple photos in a single id this is the Cs of the page that makes the register

protected void btnSalvarImovel_Click(object sender, EventArgs e)
    {
        try
        {
            var cmd = new SqlCommand("INSERT INTO Imovel(IdCidade,IdLocal,Idtipo,IdOperacao,Titulo,Obs,QntQuarto,Valor,Foto)" +
            "values(@IdCidade,@IdLocal,@Idtipo,@IdOperacao,@Titulo,@Obs,@QntQuarto,@Valor,@Foto)", con);
            cmd.Parameters.AddWithValue("@IdCidade", ddlCidade.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@IdLocal", ddlLocalizacao.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@IdTipo", ddlTipoImovel.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@IdOperacao", ddlOperacao.SelectedItem.Value);
            cmd.Parameters.AddWithValue("@Titulo", txtTitulo.Text);
            cmd.Parameters.AddWithValue("@Obs", txtObservacao.Text);
            cmd.Parameters.AddWithValue("@QntQuarto", txtQtdQuartos.Text);
            cmd.Parameters.AddWithValue("@Valor", txtValor.Text);
                foreach (var file in fuFotos.PostedFiles)
                {
                    string filename = Path.GetFileName(file.FileName);
                    file.SaveAs(Server.MapPath("~/Imovel/" + filename));
                    cmd.Parameters.AddWithValue("@Foto", file);

                }
                con.Open();
                cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

            throw new Exception("Erro ao salvar a Imóvel, atualize a página e tente novamente, se persistir o erro, contate o suporte" + ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

in case the FOREACH would be the loop to store several photos

and the following error :

inserir a descrição da imagem aqui

I would like you to stay -+ like this in the bank what can I do? any tips? inserir a descrição da imagem aqui

  • There are some problems with your code, among them: 1 - cmd.Parameters.AddWithValue("@Foto", file); you are adding several parameters "Photo" because you are inside the for; 2 - You are playing in the photo parameter the file itself and not its path on the server C:/.../...; Take a look at this and let me know if you’ve made any progress.

  • Are you trying to store the photo file in the bank? If that is a bad idea. From what I understand a property has several photos, right? Think about the way you are doing it, because you have a 1:N relationship (a table would be interesting). But you can store the photo paths using a character to separate the paths and when recurring from the database use Split to get the photo path list....

  • good evening... I managed to do... a lot of Ong to everyone ... but could help me in another problem or I have to do another post? @Jcsaint?

  • @Andréfelipejardimmo as it is another problem is better that you open a new question, so other aps can be helped if they have the same problem... :)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.