如果数据库ASP.NET中没有图像,如何隐藏图像字段 [英] How to hide image field if there is no image in database ASP.NET

查看:169
本文介绍了如果数据库ASP.NET中没有图像,如何隐藏图像字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个问题.

第一个问题.当我在数据库中为特定用户上传了图片,而不是程序运行良好并且我成功登录后,但是如果我没有上传图片,则会出现此异常. [ ^ ]

第二个问题.如果数据库中特定列中没有图像,如何隐藏图像字段.
例如,如果我在数据库中以及我的profile.aspx上有3个图像字段,并且仅上传了1张图像,我该如何隐藏其他2张图像.
希望您能理解我的担心(我的英语不太好).

我尝试过的事情:

这是我的代码:
公共局部类Control_Panel:System.Web.UI.Page
{
SqlCommand cmd =新的SqlCommand();
SqlConnection con =新的SqlConnection();
SqlDataAdapter sda =新的SqlDataAdapter();
DataSet ds = new DataSet();
受保护的void Page_Load(对象发送者,EventArgs e)
{
if(Session ["User"] == null)
{
Response.Redirect("Login.aspx");
}
其他
{
con.ConnectionString =数据源= JOSIPPC \\ SQLEXPRESS;初始目录=注册;集成安全性= True";
con.Open();
showdata();


}
}

受保护的void Button1_Click(对象发送者,EventArgs e)
{
会话[用户"] = null;
Response.Redirect("Login.aspx");
}
公共无效的showdata()
{
cmd.CommandText =从Table_1中选择*,其中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 =从Table_1中选择图像,其中Email_id ="" + Session ["user"] +''";
cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
如果(dr.HasRows)
{
while(dr.Read())
{


byte [] imgd =(byte [])dr ["Images"];
字符串图像= Convert.ToBase64String(imgd,0,imgd.Length);
Image1.ImageUrl ="data:image/png; base64," +图片;

}
}
其他
{
}
}
}

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
{
}
}
}

推荐答案

基于屏幕快照,您正在尝试将DBNUll值强制转换为字节数组,在强制转换对象之前验证null和DbNull值

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;
            }


这篇关于如果数据库ASP.NET中没有图像,如何隐藏图像字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆