Hello!
I have a code in Asp.net that calls a method in the code behind:
ASP.NET Code:<
img src="<%#CheckImage() %>" />The objective of the following code is to check if an image exists in the databse. I have created a method in the code behind(c#) that would check the image field in the database (ImageURL), if it is there, then the method should tell the ASP.net code to place the image in the browser. However, if it is not there, then the image is ignored. I just don't know how to tell the asp.net from c# to execute the code. I'm using a boolean, and I'm not sure if this is correct.
C# Code:
public bool CheckImage(){
OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=c:\\Louis\\Louis.mdb");OleDbCommand objCmd = new OleDbCommand("select * from WhiteHouse", objConn);objConn.Open();
OleDbDataReader objRdr = objCmd.ExecuteReader();
while (objRdr.Read()){
if (objRdr["ImageURL"] != null){
//For example
//meaning, go to the asp code and get the image to place it in the browser
return true;}
else
{
//meaning, don't get the image
return false;}
}
}
I would greatly appreciate some help.
Thanks,
Eduardo
Ifour HirenPosted Nov 3, 2016, 6:01 AM
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CheckImage();
}
}
public void CheckImage()
{
OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source=c:\\Louis\\Louis.mdb");OleDbCommand objCmd = new OleDbCommand("select * from WhiteHouse", objConn);
objConn.Open();
OleDbDataReader objRdr = objCmd.ExecuteReader();
while (objRdr.Read())
{
if (objRdr["ImageURL"] != null)
{
Image1.visible=true;
Image1.ImageUrl=objRdr["ImageURL"].tostring();
}
else
{
Image1.visible=false;
}
}
}
above code helps you to make it working.
Mike GoldPosted Feb 8, 2008, 6:31 PM
I wouldn't do it this way. First of all, you'll want to use the server side Microsoft control instead of the img tag. i.e.
Second, I would do your database call from within your code-behind postback or initial entry into the page. then you can just tell the Image to hide or show itself with the Visible property.