Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The Datasource of my Dropdownlist is one of the Columns in my database.i want to retrieve the column row which is selected in the dropdownlist ...in the Gridview.please help. #ASP.NET #c # gridview

What I have tried:

i am able to retrive the complete colum not thespecific rows
ASP.NET
<title>

#Select1
{
width: 197px;
}

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Album_name"
DataValueField="Album_name">
<asp:listitem>Lamborghini
<asp:listitem>BMW

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:iuploadaz %>"
SelectCommand="SELECT [Album_name] FROM [tb_Gallery]">
<asp:ScriptManager ID="ScriptManager1" runat="server">

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:AnimationExtender ID="Button1_AnimationExtender" runat="server"
Enabled="True" TargetControlID="Button1">

<asp:GridView ID="gvsearch" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Width="305px">
<columns>
<asp:BoundField DataField="Album_name" HeaderText="Album_name"
SortExpression="Album_name" />


<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Album_name" DataValueField="Album_name">

code:
C#
public partial class searchaz : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["iuploadaz"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{

// Label3.Text = a.ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from tb_Gallery", con);
// SqlDataAdapter da = new SqlDataAdapter(cmd);

// DataSet ds = new DataSet();
// da.Fill(ds, "album_name");
// DropDownList1.DataSource = ds;
// DropDownList1.DataTextField = "album_name";
//DropDownList1.DataBind();
SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like '" + DropDownList1.Text + "'", con);
//DataSet ds = new DataSet();
//da.Fill(ds);
//DropDownList1.DataSource = ds;
//DropDownList1.DataTextField = "album_name";
//DropDownList1.DataBind();
//gvsearch.DataSource = ds;
//gvsearch.DataBind();

}
}
}
Posted
Updated 1-May-18 4:24am
v3
Comments
CHill60 1-May-18 9:43am    
Show us the code you are using to retrieve "the complete column" and what you have tried so far for the specific row.
By the way - hashtagging doesn't work here ... you enter the tags as part of the initial post
Member 13802222 1-May-18 10:10am    
<title>

#Select1
{
width: 197px;
}







<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Album_name"
DataValueField="Album_name">
<asp:listitem>Lamborghini
<asp:listitem>BMW

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:iuploadaz %>"
SelectCommand="SELECT [Album_name] FROM [tb_Gallery]">
<asp:ScriptManager ID="ScriptManager1" runat="server">

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:AnimationExtender ID="Button1_AnimationExtender" runat="server"
Enabled="True" TargetControlID="Button1">

<asp:GridView ID="gvsearch" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" Width="305px">
<columns>
<asp:BoundField DataField="Album_name" HeaderText="Album_name"
SortExpression="Album_name" />


<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Album_name" DataValueField="Album_name">




....
code:
public partial class searchaz : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["iuploadaz"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{

// Label3.Text = a.ToString();
}

protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from tb_Gallery", con);
// SqlDataAdapter da = new SqlDataAdapter(cmd);

// DataSet ds = new DataSet();
// da.Fill(ds, "album_name");
// DropDownList1.DataSource = ds;
// DropDownList1.DataTextField = "album_name";
//DropDownList1.DataBind();
SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like '" + DropDownList1.Text + "'", con);
//DataSet ds = new DataSet();
//da.Fill(ds);
//DropDownList1.DataSource = ds;
//DropDownList1.DataTextField = "album_name";
//DropDownList1.DataBind();
//gvsearch.DataSource = ds;
//gvsearch.DataBind();

}
}
}
Wendelius 1-May-18 10:23am    
When adding more information, use the "Improve question" link to edit the question
Member 13802222 1-May-18 11:19am    
thanks.alright.

When you query a database, it returns all of the rows that fit your query's criteria. You therefore need a way to ask for only the rows or row that you're interested in.

The best way is to have an IDENTITY [^] column - and that is referred to as the value in your select list. Then you can pick that specific row and do what you want with it.

If it's by category, you need to mark your data in some manner whereby the category of each row is declared - then you can use the category as the value of your select list.

You can then, of course, request only the field or fields you are interested in.
 
Share this answer
 
Comments
Member 13802222 1-May-18 10:29am    
thank you for replying.
how do i mark the data?
W Balboos, GHB 1-May-18 10:33am    
If your talking about each individual row, you can add a new field of type INT, and then, per the link I gave your, convert it to an IDENTITY field. It will automatically fill with sequenced numbers.

You can now select any record for any process by referring to this value in your WHERE clause. It will always return just that record as these values are NEVER repeated. You have to determine how your list knows which record identity to associate with each item. Fill the list via a query or hard-code it.

NOTE: if you find a answer that solves your question, mark it as accepted so it can be properly closed.
First of all, do not concatenate data from controls to the SQL query. This leaves you open to SQL injection - Wikipedia[^] . The proper way is to use SqlParameter Class (System.Data.SqlClient)[^]

What comes to your query, I don't quite understand why you want to fetch the data you already have in the query below
C#
SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like '" + DropDownList1.Text + "'", con);

But if the idea is to use LIKE comparison, you probably want to have a wildcard somewhere. With parameters, something like
SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like @albumname + '%'", con);

For more information, see LIKE (Transact-SQL) | Microsoft Docs[^]

ADDITION
--------
To use a parameter you need to define it and provide a value for it. For example
...
C#
SqlDataAdapter da = new SqlDataAdapter("select album_name from tb_Gallery where album_name like @albumname + '%'", con);
da.SelectCommand.Parameters.Add(
    "@albumname ", SqlDbType.VarChar, 100).Value = DropDownList1.Text;
....
 
Share this answer
 
v2
Comments
Member 13802222 1-May-18 11:20am    
error: Must declare the scalar variable "@album_name".
Wendelius 1-May-18 11:30am    
You probably run the query without providing a value to the parameter. See the updated answer.
CHill60 1-May-18 12:08pm    
I think the OP has mistyped but also should "@albumname " be "@albumname"
Wendelius 2-May-18 23:09pm    
Good point, thank you!
CHill60 1-May-18 12:08pm    
The parameter is @albumname not @album_name - you may have mistyped from the solution.

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