abdujalil  chuliev

abdujalil chuliev

  • NA
  • 400
  • 41.2k

ddl

Sep 8 2015 1:39 AM
I am binding GridView to DropDownList.
So if i select any name from ddl, gridview has to show several images.
But in my code gridview is showing single image.
What code do I need to use to do this?
Below is my sql and cs codes.
Please help me the solve this problem.
========================================================
CREATE TABLE `images` (
`Image_ID` int NOT NULL,
`Image` blob,
PRIMARY KEY (`Image_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
==================================================================
CREATE TABLE `reg` (
`ID` int NOT NULL AUTO_INCREMENT,
`Image1_ID` int DEFAULT NULL,
`Image2_ID` int DEFAULT NULL,
`Name` varchar(45) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
==================================================================
===================================================================================================
using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ImageRetrieve : System.Web.UI.Page
{
string constr = "Data Source=localhost;port=3306;Initial Catalog=tbl;User Id=root;password=2525";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGroupdropdown();
}
}
protected void BindGroupdropdown()
{
MySqlConnection con = new MySqlConnection(constr);
con.Open();
MySqlCommand cmd = new MySqlCommand("select distinct Name from reg", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
ddlName.DataSource = ds;
ddlName.DataTextField = "Name";
ddlName.DataValueField = "Name";
ddlName.DataBind();
ddlName.Items.Insert(0, new ListItem("--Select--", "0"));
}
private void BindGrid()
{
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand("SELECT * FROM images where Image_ID='"+ getImage_ID()+"'", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
}
private int getImage_ID()
{
MySqlConnection con = new MySqlConnection(constr);
con.Open();
MySqlCommand cmd = new MySqlCommand("Select Image1_ID,Image2_ID from reg where Name='" + ddlName.SelectedItem + "'");
cmd.Connection = con;
MySqlDataReader reader = cmd.ExecuteReader();
int i = 0;
while (reader.Read())
{
foreach (DbDataRecord s in reader)
{
i = s.GetInt32(0);
i = s.GetInt32(1);
}
}
reader.Close();
return i;
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
byte[] bytes = (byte[])(e.Row.DataItem as DataRowView)["Image"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
(e.Row.FindControl("Image1") as Image).ImageUrl = "data:image/png;base64," + base64String;
}
}
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
BindGrid();
}
}

Answers (16)