Click here to Skip to main content
16,018,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSource="sqldread2"></asp:GridView>



C#
String selQuery = "SELECT Id FROM MapDataImage WHERE Source='" + TextBox1.Text + "';";
        {

            SqlCommand scmd = new SqlCommand(selQuery, con);

            con.Open();

            SqlDataReader sqldread = scmd.ExecuteReader();

            sqldread.Read();
            Dbid = (int)sqldread["Id"];
            Label4.Text = Convert.ToString(Dbid);
            sqldread.Dispose();
                if (Dbid != null)
                {
                 String QueryStr = "SELECT * FROM User_Images WHERE Id='" + Dbid + "'";
                    //String QueryStr = "SELECT * FROM User_Images GROUP BY Id HAVING ( COUNT(*) > 1 )";
                 scmd1 = new SqlCommand(QueryStr, con);
                 scmd1.CommandType = CommandType.Text;
                 scmd1.Connection = con;
                    
                    SqlDataReader sqldread2 = scmd1.ExecuteReader();
                    while (sqldread2.Read())
                    {
                        Context.Response.BinaryWrite((byte[])sqldread2["Image"]);
                        Context.Response.ContentType = "image/jpg";

                        al.Add(sqldread2);
                        sDatAdp.SelectCommand = scmd1;

                        GridView1.DataSource = sqldread2;
                        GridView1.DataBind();
                    }
                    sqldread2.Dispose();
                }


here i pass those images to gridview, but how can i view those............
the thing is i want to select all the items which related to the ID.
so for that is it my sql query is okay?
please someone check weather it's wrong or not?

example

Scenario

let say i have a database 1

[id] [name]
1 john
2 ann

if my textbox value equals to john.... it will get id from that.
so id is 1

then it will select all the images related to that particular Id from database 2

database 2

[Id] [Image]
1 image 1
2 image 2
1 image 3
1 image 4
.............

so output will be

[Id] [Image]
1 image 1
1 image 3
1 image 4

please someone help me..... i tried to do this so many times?
Posted
Updated 6-Jul-13 18:36pm
v3
Comments
promod madushan 10-Jul-13 10:44am    
Please some one tell me how to view those images at once?

1 solution

create a template field inside the grid then place another inside the template field then bind the by finding it in rowdatabound event (i-e)
C#
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" runat="server" AutoGenerateColumns="False" DataSource="sqldread2">
<Columns>
<asp:TemplateField visible="False">
<ItemTemplate><asp:label ID="lblID" runate="Server" Text='<%#Eval("Id")%>' visible="False" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
                            <ItemTemplate>                               
                              <asp:GridView ID="InnerGrid" runat="Server" AutoGenerateColumns="false">
<Columns>
    <asp:TemplateField HeaderText="images">
<ItemTemplate>
<asp:Image run="server" ID="Image1" ImageUrl='<%#Bind("SomeField")%>' width="250px" Hieght="200px"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
                            </ItemTemplate>
                        </asp:TemplateField>
<Columns>
</asp:GridView>

// code behind
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblID=e.Row.FindControl("lblID") as Label;
GridView grd=e.Row.FindControl("InnerGrid") as GridView;
if(lblID!=null && grd!=null)
{
String QueryStr = "SELECT * FROM User_Images WHERE Id='" + lblID.Text+ "'";
SqlDataAdapter da=new SqlDataAdapter(QueryStr,yourconnection);
DataTable dt=new DataTable();
da.Fill(dt);
// Request to Database and bind the innerGrid
grd.DataSource=dt;
grd.DataBind();
}


}
}
 
Share this answer
 
v3
Comments
promod madushan 7-Jul-13 1:44am    
i'll try!!!! thank you anyway......
promod madushan 7-Jul-13 1:49am    
@Zafar Safi
do i need to remove my c# code for button click event.....?
then i need to code for GridView1_RowDataBound yeah?
Zafar A khan 7-Jul-13 1:57am    
this event get call automatically with gridVeiw DataBind() no need to call it
promod madushan 7-Jul-13 1:56am    
it gives lots of errors @Zafar....

Error - Type 'System.Web.UI.WebControls.GridView' does not have a public property named 'TemplateField'.

Type 'System.Web.UI.WebControls.GridView' does not have a public property named 'ItemTemplate'.


i'm using visual studio 2012. that will be a problem yeah?
Zafar A khan 7-Jul-13 2:02am    
check your gridview > templatefield may be your given it in grid properties
it should be inside <columns></columns> tag

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