Solved

will QUERY BUILDER in SQLdatasource reads gridview cell value..??

Posted on 2008-10-30
32
1,025 Views
Last Modified: 2013-11-07
Is it possible .. to get gridview cell value in the querybuilder of sqldatasource control.

My issue is . am using a gridview, in that there r three columns .
namely
id,name,quantity
where as for name field am  using dropdown list,
My Criteria...
1.am populating name  from db in dropdown list using sqldatasource ctrl . which is too ec . But wat really not happening is . i want to bind dropdown according to  id field index value in gridviw.. in otherwords I want to populate by using the gridview cell of 0 value ...is it possible..to in query builder. . ??


For eg ...(this is just for understanding like this...).
select name from table where id= row.Cells(0).Text) ...??

.can v get index value of gridview in querybuilder in sqldatasource ctrl....????

if its not possible y...???Plz suggest ....



0
Comment
Question by:Rajar Ahmed
  • 19
  • 13
32 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22848367
If you really want to use QueryBuiler this way then:

select name from table where id= <%#DataBinder.Eval( Container.DataItem, "ID" )%>

Otherwise, refer to another solutions of mine which programatically form code behind:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q__23864429.html

Hope this helps

JINN

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848450
if i used this <%#DataBinder.Eval( Container.DataItem, "ID" )%>,am getting error , of syntax error of converting varchar to int ..kindly suggest
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848461
well now trying ur program, i ll get to no wat is comin ....
Kindly help.....If der was ny erro ,

 jst writing the code for according for my issue

private void PopulateDropDownListNameByID(string ID, DropDownList ddlname)
{
    //query and populate ddlName
}


Thanks....lll let u no soon,......
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22848499
if i used this <%#DataBinder.Eval( Container.DataItem, "ID" )%>,am getting error , of syntax error of converting varchar to int ..kindly suggest

--> it means it gets the data out no problem, it's  just casting, you might wanna try around with these cast and convert. Is that SQL error, or compiler error.

Try another method and tell me if you need further assistance

JINN
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848540
select id,(select ame from table where id=othrtable.id)as resname from othrtable where id=<%#DataBinder.Eval( Container.DataItem, "ID" )%>


this i entered in querybuilder , which returned  that error..



0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848552
the below u given for datagrid, if its gridview means , wthr i should use databound or databind Event . ??



 protected void dgUserPasswords_ItemDataBound(object sender, DataGridItemEventArgs e)

{

        // Check if the current row contains items; if it's a header or footer row that will throw an error

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

                //get the ID

                string id = DataBinder.Eval(e.Item.DataItem, "ID").ToString();

                

                //find the current dropdown list:

                DropDownList ddlName = e.Item.FindControl("ddlName");

                

                //NOW you have the current ID and the dropdownlist -> populate the dllName dropdownlist 

                PopulateDropDownListNameByID(id, ddlName);

        }

}

 

private void PopulateDropDownListNameByID(string ID, DropDownList ddlname)

{

    //query and populate ddlName

}

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848622
I did the followin code,but cudn retrive ...
As i need resname in (dropdown list) value with fid index...

i ve three tables

1.foodtable field                                            
fid int(identity(1,1),primary)
fname varchar                                                        

table 2:Restauranttable          
1.Rid primary key                                                  
2. resname varchar                                                      

table3 : pricetable                                                    
1.pid int(identity(1,1),primary)                                    
2.fid int foriegn key(foodtable)
3..rid  int  foreign key(resttable)
4.price float    


My gridview

fid(maintable) resname(resttable) in dropdown

Plz helpme .. to retreive details in dropdown list ..

Thanks...

 
protected void GridView1_DataBound(object sender, EventArgs e)

    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

            //get the ID

            string id = DataBinder.Eval(e.Item.DataItem, "fid").ToString();
 

            //find the current dropdown list:

            DropDownList ddlName = e.Item.FindControl("DropdownList1");
 

            //NOW you have the current ID and the dropdownlist -> populate the dllName dropdownlist

            PopulateDropDownListNameByID(id, ddlName);

        }
 

    }

    private void PopulateDropDownListNameByID(string ID, DropDownList ddlname)

    {

        //query and populate ddlName
 

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("Select fid,(select resname from restable where rid=maintable.rid)as resname from maintable", con);

        da.Fill(ds);

        return ds;

    }

Open in new window

0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22848630
can you trace down whether it is SQL error or Compiler error ?? Which type the field ID is? varchar or int? Int I guess right?
Try:
<%# Int32.Parse(DataBinder.Eval( Container.DataItem, "ID" )) %>

The GridView DataBound will be fired after all the row has been bounded to gridview from datasource. You can use this to change footer text (such as summary infor ...)

There are orders in which you can find the best moment to fulfill your wish, for your case, using RowDataBound is the best option.

Refer to this article to have more information
http://weblogs.asp.net/meligy/archive/2008/01/25/gridview-databinding-events.aspx

Hope this helps

JINN
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22848650
What you did was returning a DataSet while the PopulateDropDownListNameByID is a method return void, this is a custom method as my suggestion you can do:

And you have to do this in RowDataBound (as each grid row is binded). Go to GridView property to set this event

You also have to configure the GridView properly in your aspx file. (with databoundField etc.)

Hope this helps

JINN


private void PopulateDropDownListNameByID(string ID, DropDownList ddlname)

    {

        //query and populate ddlName

 

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("Select fid,(select resname from restable where rid=maintable.rid)as resname from maintable", con);

        da.Fill(ds);

      

        ddlname.Items.Clear();

        ddlname.DataSource = ds.Tables[0];

        ddlname.DataValueField = "resname";

        ddlname.DataTextField = "resname";

        ddlname.DataBind();

    }

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848756

Can u able to understand my issue, if not ll tell again, okay ..below are the error i got when i used this code which u said b4,

Plz tell necesary changes to take ....Thanks lukin frwd for ur help////.......



Error    1    'System.Web.UI.WebControls.GridViewRowEventArgs' does not contain a definition for 'Item'  
Error    2    'System.Web.UI.WebControls.GridViewRowEventArgs' does not contain a definition for 'Item'  
Error    3    'System.Web.UI.WebControls.GridViewRowEventArgs' does not contain a definition for 'Item'  
Error    4    'System.Web.UI.WebControls.GridViewRowEventArgs' does not contain a definition for 'Item'  
Error    5    The name 'fid' does not exist in the current context    










public partial class griduser : System.Web.UI.Page

{

    //int fidnew;

    protected void Page_Load(object sender, EventArgs e)

    {
 

        if (!IsPostBack)

        {

            //GridView1.DataSource = resnameddl1().Tables[0];

            GridView1.DataBind();

        }

    }

    //public DataSet resnameddl1()

    //{

    //    string constr = ConfigurationManager.AppSettings["constring"];

    //    SqlConnection myconnection = new SqlConnection(constr);

    //    DataSet ds = new DataSet();
 

    //    string cmd = "select distinct fid,(select fname from foodtable where fid=maintable.fid) as fname,dbo.resname(fid) as resname  from maintable";

    //    SqlDataAdapter da = new SqlDataAdapter(cmd, myconnection);

    //    da.Fill(ds, "maintable");

    //    return ds;

    //}

    //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    //{
 

    //    DataTable mytable = new DataTable();

    //    DataColumn restid = new DataColumn("rid");

    //    DataColumn restname1 = new DataColumn("resname1");

    //    mytable.Columns.Add(restid);

    //    mytable.Columns.Add(restname1);

    //    DataSet ds = new DataSet();

    //    ds = resnameddl1();

    //    int fid = 0;

    //    string expression = string.Empty;

        

    //    string rnames;

    //    if (e.Row.RowType == DataControlRowType.DataRow)

    //    {

    //        fid = Int32.Parse(e.Row.Cells[0].Text);

    //        expression = "fid = " + fid;

    //        DropDownList ddl = (DropDownList)e.Row.FindControl("dropdownlist1");

    //        DataRow[] rows = ds.Tables[0].Select(expression);

    //        rnames = "";

    //        DataRow newrow = mytable.NewRow();

    //        foreach (DataRow row in rows)

    //        {
 

    //            //string temprest = "resname1";

    //            //string[] temprestarr = temprest.Split(',');

    //            fidnew = Convert.ToInt32(row["fid"]);

    //            rnames = rnames + row["resname"] + ",";

    //        }

          

    //        newrow["rid"] = Convert.ToInt32(fidnew);

    //        newrow["resname1"] = rnames;

    //        mytable.Rows.Add(newrow);
 

    //        ddl.DataSource = mytable;

    //        ddl.DataTextField = "resname1";

    //        ddl.DataValueField = "rid";

    //        ddl.DataBind();

    //    }

    //}
 

    //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    //{
 

    //    DataSet ds = new DataSet();

    //    ds = resnameddl1();

    //    if (e.Row.RowType == DataControlRowType.DataRow)

    //    {

    //        DropDownList ddl = (DropDownList)e.Row.FindControl("dropdownlist1");

    //        ddl.DataSource = ds;

    //        ddl.DataTextField = "resname";

    //        ddl.DataValueField = "fid";

    //        ddl.DataBind();

    //    }

    //}
 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

            //get the ID

            string id = DataBinder.Eval(e.Item.DataItem, "fid").ToString();
 

            //find the current dropdown list:

            DropDownList ddlName = e.Item.FindControl("DropdownList1");
 

            //NOW you have the current ID and the dropdownlist -> populate the dllName dropdownlist

            PopulateDropDownListNameByID(fid, ddlName);

        }
 
 

    }
 
 
 
 
 
 

    //   protected void Button1_Click(object sender, EventArgs e)

    //{

    //    Response.Redirect("maintable.aspx");

    //}
 
 
 

    private void PopulateDropDownListNameByID(string fid, DropDownList ddlname)

    {

        //query and populate ddlName

        string constr = ConfigurationManager.AppSettings["constring"];

        SqlConnection myconnection = new SqlConnection(constr);

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("Select fid,(select resname from restable where rid=maintable.rid)as resname from maintable", myconnection);

        da.Fill(ds);
 

        ddlname.Items.Clear();

        ddlname.DataSource = ds.Tables[0];

        ddlname.DataValueField = "resname";

        ddlname.DataTextField = "resname";

        ddlname.DataBind();

    }

    }

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848777
i think , previous  one ll be confusing below code is wat  is required things u ve to luk...
WHich r things i changed as ur valuable sayings...........

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
 

public partial class griduser : System.Web.UI.Page

{

    //int fidnew;

    protected void Page_Load(object sender, EventArgs e)

    {
 

        if (!IsPostBack)

        {

            //GridView1.DataSource = resnameddl1().Tables[0];

            GridView1.DataBind();

        }

    }

    
 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

        {

            //get the ID

            string id = DataBinder.Eval(e.Item.DataItem, "fid").ToString();
 

            //find the current dropdown list:

            DropDownList ddlName = e.Item.FindControl("DropdownList1");
 

            //NOW you have the current ID and the dropdownlist -> populate the dllName dropdownlist

            PopulateDropDownListNameByID(fid, ddlName);

        }
 
 

    }
 

    private void PopulateDropDownListNameByID(string fid, DropDownList ddlname)

    {

        //query and populate ddlName

        string constr = ConfigurationManager.AppSettings["constring"];

        SqlConnection myconnection = new SqlConnection(constr);

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("Select fid,(select resname from restable where rid=maintable.rid)as resname from maintable", myconnection);

        da.Fill(ds);
 

        ddlname.Items.Clear();

        ddlname.DataSource = ds.Tables[0];

        ddlname.DataValueField = "resname";

        ddlname.DataTextField = "resname";

        ddlname.DataBind();

    }

    }
 
 
 

    <div><asp:GridView ID="GridView1" runat="server" style="left: 157px; position: relative; top: 141px" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" >

            <Columns>

                <asp:BoundField DataField="fid" HeaderText="fid" />

                <asp:BoundField HeaderText="Fname" DataField="fname"/>

                <asp:TemplateField HeaderText="rname">

                    <ItemTemplate>

                        <asp:DropDownList ID="DropDownList1" runat="server" Style="position: relative">

                        </asp:DropDownList>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:BoundField HeaderText="price" />

                <asp:TemplateField HeaderText="Quantity">

                    <EditItemTemplate>

                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                    </EditItemTemplate>

                    <ItemTemplate>

                        <asp:TextBox ID="TextBox2" runat="server" Style="position: relative"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:ButtonField CommandName="addcart" HeaderText="ADDCART" Text="Button" />

            </Columns>

        </asp:GridView>

Open in new window

0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22848818
Sorry my bad, I dont have my compiler running right now, here is the correction

Jinn

PS: I do understand your issues (we have had quite a lot of similar cases ^^)





protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if(e.Row.RowType == DataControlRowType.DataRow)

        {

            //get the ID

            string id = e.Row.Cells[0].Text;

 

            //find the current dropdown list:

            DropDownList ddlName = e.Row.FindControl("DropdownList1") As DropDownList;

 

            //NOW you have the current ID and the dropdownlist -> populate the dllName dropdownlist

            PopulateDropDownListNameByID(fid, ddlName);

        }

 

 

    }
 

private void PopulateDropDownListNameByID(string fid, DropDownList ddlname)

    {

        //query and populate ddlName

        string constr = ConfigurationManager.AppSettings["constring"];

        SqlConnection myconnection = new SqlConnection(constr);

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("Select fid,(select resname from restable where rid=maintable.rid)as resname from maintable", myconnection);

        da.Fill(ds);

 

        ddlname.Items.Clear();

        ddlname.DataSource = ds.Tables[0];

        ddlname.DataValueField = "fid";

        ddlname.DataTextField = "resname";

        ddlname.DataBind();

    }

Open in new window

0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22848833
Gotcha, extremely about the confusion before, I was refering to DataGrid instead of GridView, >.< as we have a lot of different controls and I took one so you can have an idea, accidentally, I got the DataGrid instead >.<

The correction above should work

JINN
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848897
for this line---->>>PopulateDropDownListNameByID(fid, ddlName);

  ERROR.....The name 'fid' does not exist in the current context    


Thnkss hor helpin so far, .....i had changed many things so far,

Now code is totaly diff as i did b4.. plz enlightn mistake if there any .....




public partial class griduser : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {
 

---------- DO I NEED ANY CODE  HERE ---       

    }

    
 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if(e.Row.RowType == DataControlRowType.DataRow)

        {

            //get the ID

            string id = e.Row.Cells[0].Text;

 

            //find the current dropdown list:

            DropDownList ddlName = (DropDownList)e.Row.FindControl("dropdownlist1");

 

            //NOW you have the current ID and the dropdownlist -> populate the dllName dropdownlist

            PopulateDropDownListNameByID(fid, ddlName);<<<-----ERROR LINE

        }

 

 

    }

        private void PopulateDropDownListNameByID(string fid, DropDownList ddlname)

    {

        //query and populate ddlName

        string constr = ConfigurationManager.AppSettings["constring"];

        SqlConnection myconnection = new SqlConnection(constr);

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("Select fid,(select resname from restable where rid=maintable.rid)as resname from maintable", myconnection);

        da.Fill(ds);
 

        ddlname.Items.Clear();

        ddlname.DataSource = ds.Tables[0];

        ddlname.DataValueField = "rid";

        ddlname.DataTextField = "resname";

        ddlname.DataBind();

    }

    }
 

Please do ,check wthr, my aspx is correct....!!!
 

<div><asp:GridView ID="GridView1" runat="server" style="left: 157px; position: relative; top: 141px" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False" >

            <Columns>

                <asp:BoundField DataField="fid" HeaderText="fid" />

                <asp:BoundField HeaderText="Fname" DataField="fname"/>

                <asp:TemplateField HeaderText="rname">

                    <ItemTemplate>

                        <asp:DropDownList ID="DropDownList1" runat="server" Style="position: relative">

                        </asp:DropDownList>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:BoundField HeaderText="price" />

                <asp:TemplateField HeaderText="Quantity">

                    <EditItemTemplate>

                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                    </EditItemTemplate>

                    <ItemTemplate>

                        <asp:TextBox ID="TextBox2" runat="server" Style="position: relative"></asp:TextBox>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:ButtonField CommandName="addcart" HeaderText="ADDCART" Text="Button" />

            </Columns>

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848939

 
 select id,(select name from table where id=othrtable.id)as resname from othrtable where id=<%#DataBinder.Eval( Container.DataItem, "ID" )%>
 
 
 Plz format this code as u say .....how to change accordin for gridview. ...???i din get yaar srryy...
Wats THIS >.< ????????
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22848951
Hi,

Line 15 should be fid instead of id, you can use the compiler to verify the syntax error.

        if(e.Row.RowType == DataControlRowType.DataRow)
        {
                 //get the ID
                 string fid = e.Row.Cells[0].Text;

In PageLoad: it depends on what do you want to do when the page is Load or postback. In page life circle this Page_Load method is fired before Button_Click, SelectedIndexChange, RowDataBouond are called.

So in another scenarios, for example: when you have dynamicontrols added, they will be done here otherwise their buttons will not be register.

The Page_Load will always be fired as the page is call. In your case, I haven't seen anything that you need to do there.

Try put:
this.Title = "Users page"; in page_Load and put a break point there, debug, you will see it will be fired before RowDataBound event is fired ^^

Hope this give you some idea

JINN

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22848982
Now i see a empty page........ No Error But,,,,.CAnt able to c grid


Not able to c my grid ... WHere am i wrong...???

Plz tell...





 protected void Page_Load(object sender, EventArgs e)

    {

    }

    
 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if(e.Row.RowType == DataControlRowType.DataRow)

        {

            //get the ID

            string fid = e.Row.Cells[0].Text;

 

            //find the current dropdown list:

            DropDownList ddlName = (DropDownList)e.Row.FindControl("dropdownlist1");

 

            //NOW you have the current ID and the dropdownlist -> populate the dllName dropdownlist

            PopulateDropDownListNameByID(fid, ddlName);

        }

 

 

    }

        private void PopulateDropDownListNameByID(string fid, DropDownList ddlname)

    {

        //query and populate ddlName

        string constr = ConfigurationManager.AppSettings["constring"];

        SqlConnection myconnection = new SqlConnection(constr);

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("Select fid,(select resname from restable where rid=maintable.rid)as resname from maintable", myconnection);

        da.Fill(ds);
 

        ddlname.Items.Clear();

        ddlname.DataSource = ds.Tables[0];

        ddlname.DataValueField = "rid";

        ddlname.DataTextField = "resname";

        ddlname.DataBind();

    }

    }

Open in new window

0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22849016
I am not so sure what error do you have with that line, you haven't  tell me if the FID column in your database is Int or Varchar, and what error you got from (SQL or VS)

ok try this:
 select id,(select name from table where id=othrtable.id)as resname from othrtable where id=<%# Convert.ToInt32(DataBinder.Eval( Container.DataItem, "ID")) %>

or if FID is varchar:
 select id,(select name from table where id=othrtable.id)as resname from othrtable where id='<%# DataBinder.Eval( Container.DataItem, "ID").ToString() %>'

Test around with it

JINN
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22849052
Ok, then it is good now. Your problem in the new page is you haven't bind the gridView in any datasource, bind it first (bind to objectDataSource or dynamically from a dataset somewhere). Look at my example, change your query accordingly

Make sure everytime you Open and Close the connection

JINN





protected void Page_Load(object sender, EventArgs e)

{
 

	if (!IsPostBack)

	{

	       

		 //query and populate ddlName

	        string constr = ConfigurationManager.AppSettings["constring"];

	        SqlConnection myconnection = new SqlConnection(constr);

	        DataSet ds = new DataSet();

		myconnection.Open()

	        SqlDataAdapter da = new SqlDataAdapter("Select * from Users", myconnection);

	        da.Fill(ds);

		

		GridView1.DataSource = ds.Tables[0];

		GridView1.DataBind();

		myconnection.Close();

	}

}

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22849132
my error is in VS query buider of sqldatasource contrl...

Fid is int
So, i used
select id,(select name from table where id=othrtable.id)as resname from othrtable where id=<%# Convert.ToInt32(DataBinder.Eval( Container.DataItem, "ID")) %>

ERror is now
Ensure select command properties is valid(its A message box while clicking test query)
Line 1 : Incorrect  syntax near '<'.
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22849197
While using code, am gettin

o/p was like this in gridview

fid  Resname(Dropdown LIST)
56   Restaurant A RestaurantB Restaurant
56   Restaurant A RestaurantB Restaurantc
56    Restaurant A RestaurantB Restaurantc
56  Restaurant A RestaurantB Restaurantc
63  Restaurant A RestaurantB Restaurantc
63   Restaurant A RestaurantB Restaurantc
63    Restaurant A RestaurantB Restaurantc
63    Restaurant A RestaurantB Restaurantc
65   Restaurant A RestaurantB Restaurantc

THIS IS MY REQUIREMENT ::::::
 REsname( ie dropdown List ) (Clarity More : That i need to populate Resname w.r.to fid , if    restaurant C is not present in fid 63 means Restaurant C should not Get into dropdown list  of fid 63)


I need LIKE THIS,,,,,,
fid   Resname
56  Restaurant A RestaurantB Restaurantc
63  RestaurantA,REstaurantC
65  RestaurantC
This is my issue......Waitin for Suggestion........

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22849289
hi .. Good job....

I was able to retrieve using distinct fid ,,


Now last thing which s happening is  

am not gettin with index value of fid ..its retrieving my all records of restaurant in dropdown list....

Plz suggest wat can be done.....





0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22849352
over here:

if(e.Row.RowType == DataControlRowType.DataRow)
        {
      //get the ID
                string fid = e.Row.Cells[0].Text;

the cell index, you just gotta make sure it gets the right number (looks like it 0 but I dont know what you have infront of it, you can try 1 2 (incase you have edit button before that column)
string fid = e.Row.Cells[2].Text; for example

Put a debug point in there, try to what value it gets.

JINN


0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22849420
ITs reading CEll VAlue...

Am gettin Damn right cell index valuess...


The only issue is...
with this query i gues....

select fid,(select resname from resttable where rid = maintable.rid)as resname from maintable

This is the query am passing to ddlname ...Ders mistake inside in dis query oly ....

Wats Happenin ITs Retriving all the records to every dropdown list of resname

For eg..
fid
1 as resname 1
2 as resname 1,resname2
3. as resname1
                                                                                                Everey dropdownLIST been populated  having values Of resname1,resname1,resname2,resname1


Plz check with the query...u ve been miraculously helpful so far....

Thakssssss a lot......


.

  private void PopulateDropDownListNameByID(string fid, DropDownList ddlname)

    {

        //query and populate ddlName

        string constr = ConfigurationManager.AppSettings["constring"];

        SqlConnection myconnection = new SqlConnection(constr);

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("select fid,(select resname from resttable where rid = maintable.rid)as resname from maintable", myconnection);

        da.Fill(ds);

        ddlname.Items.Clear();

        ddlname.DataSource = ds.Tables[0];

        ddlname.DataValueField = "fid";

        ddlname.DataTextField = "resname";

        ddlname.DataBind();

    }

Open in new window

0
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
ID: 22849530
What you should do is using inner join for the same type of query, and ADD the WHERE condition for FID.

By the way, is that the RID column is the foreign key ???

Then the way I see it the ddlname.DataVlueField will have to be resname, otherwise the value will always be the same (as declared = fid)

replace by this code:


DataSet ds = new DataSet();
 

string query = "SELECT fid, resname FROM maintable INNER JOIN resttable ON restable.rid = maintable.rid WHERE maintable.fid = " + fid

SqlDataAdapter da = new SqlDataAdapter(query, myconnection);
 

da.Fill(ds);

ddlname.Items.Clear();

ddlname.DataSource = ds.Tables[0];

ddlname.DataValueField = "resname";

ddlname.DataTextField = "resname";

ddlname.DataBind();

Open in new window

0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22849572
HEyyyyyyyy THankssssss

U deservee many  points than 500 . .......
I was trying this for more than 2 weeks......................


No words..... U guysss Rockkkssss.......

Luking for more help.........


Thanksssssssss a lottttttttttttttttttttttt
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22849588
HEyyyyyyyy THankssssss

U deservee many  points than 500 . .......
I was trying this for more than 2 weeks......................


No words..... U Rockkkssss.......

Luking for more help.........


Thanksssssssss a lottttttttttttttttttttttt
0
 
LVL 18

Author Closing Comment

by:Rajar Ahmed
ID: 31511940
GReat Explanation...
Very good clarity
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22849676
You are welcome, I'd like to think you would do the same for another who might need helps. It's great you work it out.

Glad to help ^^

JINN
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22849842
definitely. i f am confident
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22849862
You are and you will be even more ^^

This is the right place ^^

JINN
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22850171
Since am dealing with diff issue, i had posted my new question, this link ll take u to c my isue

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23865101.html

Thank u...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now