Leave image by default ASP .NET

2

Good morning. You see, I'm doing a project that includes a profile, and that profile has the option of adding an image. The issue here is that it saves the path of the image in BD and the image in the folder. But when creating a new user, the Image control is null. How can I make this image by default when creating a new user?

This is the method to consult BD image:

    protected void ConsultarImagen()
    {
        SqlConnection conexion = new SqlConnection(cadena);
        SqlCommand com = new SqlCommand();
        string id = Session["IdUsuario"].ToString();
        lblId.Text = id;
        com.CommandText = "Select ruta_imagen from tb_c_usuarios where id=" + id;
        com.CommandType = CommandType.Text;
        com.Connection = conexion;
        conexion.Open();
        var dataReader = com.ExecuteReader();
        DataTable dataTable = new DataTable();
        dataTable.Load(dataReader);

        if(dataTable!= null && dataTable.Rows.Count > 0)
        {
            string PathImagen = dataTable.Rows[0]["ruta_imagen"].ToString();
            string URLPath = "http://" + Request.Url.Authority + "/" + PathImagen;

            imagenPerfil.ImageUrl = URLPath;
        }

        conexion.Close();
        imagenPerfil.DataBind();
    }
    
asked by Yarizza 18.05.2018 в 16:47
source

1 answer

1

Create a default image in a public folder, when you create a new row in tb_c_usuarios make path_image the path to that default folder,

that you can achieve with a trigger, although I would do it directly in the script that creates the new user

The easy method would be to modify the code directly and add an else

protected void ConsultarImagen()
{
    SqlConnection conexion = new SqlConnection(cadena);
    SqlCommand com = new SqlCommand();
    string id = Session["IdUsuario"].ToString();
    lblId.Text = id;
    com.CommandText = "Select ruta_imagen from tb_c_usuarios where id=" + id;
    com.CommandType = CommandType.Text;
    com.Connection = conexion;
    conexion.Open();
    var dataReader = com.ExecuteReader();
    DataTable dataTable = new DataTable();
    dataTable.Load(dataReader);

    if(dataTable!= null && dataTable.Rows.Count > 0)
    {
        string PathImagen = dataTable.Rows[0]["ruta_imagen"].ToString();
        string URLPath = "http://" + Request.Url.Authority + "/" + PathImagen;

        imagenPerfil.ImageUrl = URLPath;
    }
    else
    {
        string URLPath = "http://folders/folders/imagenpredeterminada.png"
        imagenPerfil.ImageUrl = URLPath;
    {

    conexion.Close();
    imagenPerfil.DataBind();
}

I prefer the option to have the location of the default image from the beginning in the database to avoid rare encodings like the one above.

    
answered by 18.05.2018 / 16:52
source