Hello forum,
I have a GridView where I link multi-tables to display data on it. I select data to display when I click on a value inside the dropdown list and between dates.
Some tables have image column/data and some tables don't have image data.
So when I applied the code to display images; it displayed images from one table and when I select another table (where no image is stored) from dropdown list I get an error.
image is neither a DataColumn nor a DataRelation for table
protected void Button2_Click(object sender, EventArgs e)
{
if (ddlTables.SelectedIndex > 0)
{
string query = "";
if (ddlTables.SelectedValue.ToLower() == "cards")
{
query = "SELECT TOP 5 email,UserRole,Name,image,CreateDate FROM Users WHERE (CreatedBy=@CreatedBy OR CreatedBy = '')";
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
query += " AND CreateDate BETWEEN @From AND @To";
}
}
if (ddlTables.SelectedValue.ToLower() == "certificates")
{
query = "SELECT TOP 5 Name,Mobile,VehicleVin,Type,Date,Location,ReportDate FROM VehicleReport";
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
query += " WHERE ReportDate BETWEEN @From AND @To";
}
}
if (ddlTables.SelectedValue.ToLower() == "Reciepts")
{
query = " ";
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
query += " WHERE BETWEEN @From AND @To";
}
}
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@CreatedBy", user.Text);
if (!string.IsNullOrEmpty(TextBox1.Text) && !string.IsNullOrEmpty(Dat.Text))
{
cmd.Parameters.AddWithValue("@From", TextBox1.Text);
cmd.Parameters.AddWithValue("@To", Dat.Text);
}
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dr = (DataRowView)e.Row.DataItem;
string imageUrl = "data:image/jpg;base64," + Convert.ToBase64String((byte[])dr["image"]);
(e.Row.FindControl("Image1") as Image).ImageUrl = imageUrl;
}
}