Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 questions.

First problem. When i have uploaded image in my database for specific user than my program working good and i successful log in , but if i dont have uploaded image i got this exception. [^]

And second question. How can i make it to hide image field if there is no image in specific column in database.
For example if i have 3 image fields in database and on my profile.aspx and only 1 image is uploaded , how i can hide other 2.
I hope you understand my concern(my english is not perfect).

What I have tried:

This is my code:
public partial class Control_Panel : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if(Session["User"] == null )
{
Response.Redirect("Login.aspx");
}
else
{
con.ConnectionString = "Data Source=JOSIPPC\\SQLEXPRESS;Initial Catalog=register;Integrated Security=True";
con.Open();
showdata();


}
}

protected void Button1_Click(object sender, EventArgs e)
{
Session["user"] = null;
Response.Redirect("Login.aspx");
}
public void showdata()
{
cmd.CommandText = "Select* from Table_1 where Email_id = '" +Session["user"]+ "'";
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(ds);
Label1.Text = ds.Tables[0].Rows[0]["First_Name"].ToString() + " "+ ds.Tables[0].Rows[0]["Last_Name"].ToString();
Label2.Text = ds.Tables[0].Rows[0]["Email_id"].ToString();

cmd.CommandText = "select Images from Table_1 where Email_id='" + Session["user"] + "'";
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{


byte[] imgd = (byte[])dr["Images"];
string images = Convert.ToBase64String(imgd, 0, imgd.Length);
Image1.ImageUrl = "data:image/png;base64," + images;

}
}
else
{
}
}
}
Posted
Updated 22-Jan-18 7:31am
Comments
Richard Deeming 23-Jan-18 12:39pm    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

using (var con = new SqlConnection("Data Source=JOSIPPC\\SQLEXPRESS;Initial Catalog=register;Integrated Security=True"))
using (var cmd = new SqlCommand("Select First_Name, Last_Name, Email_Id, Images from Table_1 where Email_id = @user", con))
{
    cmd.Parameters.AddWithValue("@user", Session["user"]);
    
    con.Open();
    
    using (var reader = cmd.ExecuteReader())
    {
        if (reader.Read())
        {
            Label1.Text = string.Format("{0} {1}", reader["First_Name"], reader["Last_Name"]);
            Label2.Text = reader["Email_Id"].ToString();
            
            object img = reader["Images"];
            if (img != null && !Convert.IsDBNull(img))
            {
                ...
            }
        }
    }
}

1 solution

based on the screenshot, you are trying to cast the DBNUll Value to byte array, Validate for null and DbNull value before casting the object

Image1.Visible = false;
            object img = dr["Images"];
            if (img != null && img != DBNull.Value)
            {
                Image1.Visible = true;
                byte[] imgd = (byte[])dr["Images"];
                string images = Convert.ToBase64String(imgd, 0, imgd.Length);
                Image1.ImageUrl = "data:image/png;base64," + images;
            }
 
Share this answer
 
v2
Comments
Gatsby29 22-Jan-18 16:20pm    
Thank you a lot it works great
Karthik_Mahalingam 22-Jan-18 21:34pm    
Welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900