Solved

Need to split records in dropdownlist

Posted on 2008-10-28
3
560 Views
Last Modified: 2013-12-17
i want to split records in dropdown list,

O/p is :
fid                   resname(dropdown list )
1                     RestaurantA,RestaurantB,RestaurantC,RestaurantD(it displays like ths only having all records in zero index , but dropdown shuould be populated by varying its index values from zero to three if it has 4 records )
3                     RestaurantD
4                     RestaurantA,RestaurantB(similarly for this)
5                     RestaurantB
9                     RestaurantA, RestaurantC(simalarly)
11                   RestaurantD



I think , am cleared with this one .

Only thing is, i need a split function , so that it populate the record one by one in dropdown list having diff index values .


Kindly suggest me ur valuable idea,...

Please if i made any mistake in codin , u can advice me wat to do ......
<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" />

                <asp:TemplateField HeaderText="rname">

                    <EditItemTemplate>

                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("resname") %>'></asp:TextBox>

                    </EditItemTemplate>

                    <ItemTemplate>

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

                        </asp:DropDownList>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:BoundField HeaderText="price" />

            </Columns>

        </asp:GridView>

          </div>

 

 

SErver side code( i want split function)

 protected void Page_Load(object sender, EventArgs e)

    {

        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,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;

        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);

            foreach (DataRow row in rows)

            {

                DataRow newrow = mytable.NewRow();

                string temprest = "resname";

                char[] sep = { ',' };

                string[] values = temprest.Split(sep);

                newrow["rid"] = row["fid"];

                newrow["resname1"] = row["resname"];

                mytable.Rows.Add(newrow);

 

            }

 

            ddl.DataSource = mytable;

             ddl.DataTextField = "resname1";

            ddl.DataValueField = "rid";

            ddl.DataBind();

        }

    }

Open in new window

0
Comment
Question by:Rajar Ahmed
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
aliwajdan earned 500 total points
ID: 22828977
protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = resnameddl1().Tables[0];
        GridView1.DataBind();
 
    }
there is mistake in your  Page_Load function

use
protected void Page_Load(object sender, EventArgs e)
    {
     if(!Page.IsPostBakc)
         {
        GridView1.DataSource = resnameddl1().Tables[0];
        GridView1.DataBind();
       }
    }

you'll not need any split function  or any thing else when you add items in the dropdown list it do these things autometically like making automatic index columns
 
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22828991
hi , it shows with the same sort of displaying in dropdown with the use of postback also as u mentioned..
plz kindly suggest me any othr views ..

Thanks ..
0
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 22829028

this function i used to retrieve values for dropdown list  which is resname,



 

Create FUNCTION dbo.resname(@fid int)
 

RETURNS VARCHAR(8000)
 

AS
 

BEGIN
 

DECLARE @keyword_Name VARCHAR(4000)
 

SET @keyword_Name = ''

SELECT @keyword_Name =
 

(CASE @keyword_Name
 

WHEN '' THEN resname
 

ELSE @keyword_Name + ',' +resname
 

END)
 

from MainTable
 

inner join Resttable rest
 

on MainTable.rid= rest.rid
 

where fid=@fid
 

RETURN @keyword_Name
 

END
 

REsult is

fid  Resname(this field am storing in dropdown list)

56   Restaurant B,Restaurant C,Restaurant D	

63   Restaurant C,Restaurant A	

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 47
C# HTTP GET method sample code 3 42
C# Single Form 8 29
free scanner TWAIN can be operated with a Web application 9 41
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

26 Experts available now in Live!

Get 1:1 Help Now