Solved

sqldatasource control doubt

Posted on 2008-10-31
4
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
SameerJagdale earned 500 total points
ID: 22848195
i think you need to create a parameterized query. have a look at the article below:
http://www.asp.net/Learn/data-access/tutorial-48-vb.aspx
i suggest you can have a hidden field that will hold the cell value and provide this hidden field as a control to your parameterized query.. have a look at the article and it will make sense to you.
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22848334
You can do it in several way, but I recommend to use TemplateField which you have free control of what happens to your gridview and how you want to bind/display it.

<asp:GridView ..>
..
<asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                       <asp:DropDownList ID="ddlName" runat="server" />
                </ItemTemplate>
 </asp:TemplateField>
</asp:GridView>

The next the step is: in your code behind, you populate this Dropdownlist upon the ID, the best place to do that is in RowDataBound. Look at the code

hope this gives you the idea

JINN




  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 26

Expert Comment

by:Anurag Thakur
ID: 22857318
the best and the easiest of methods for doing an update will be by using the datakeynames property for the gridview
set you id as the datakeyname for your gridview and have a look at the following code sample

this sample code i have taken from the following link
http://asp.dotnetheaven.com/aspnet/doc/ctrlref/data/sqldatasource.aspx from C# GridView Updating section in it

hope it helps - ragi
<%@ Page Language="VB" %>
<html>
  <head runat="server">
    <title>Updating Data Using GridView</title>
  </head>
  <body>
    <form id="form1" runat="server">
      <asp:GridView ID="GridView1" AllowSorting="true" AllowPaging="true" Runat="server"
        DataSourceID="SqlDataSource1" AutoGenerateEditButton="true" DataKeyNames="au_id"
        AutoGenerateColumns="False">
        <Columns>
          <asp:BoundField ReadOnly="true" HeaderText="ID" DataField="au_id" SortExpression="au_id" />
          <asp:BoundField HeaderText="Last Name" DataField="au_lname" SortExpression="au_lname" />
          <asp:BoundField HeaderText="First Name" DataField="au_fname" SortExpression="au_fname" />
          <asp:BoundField HeaderText="Phone" DataField="phone" SortExpression="phone" />
          <asp:BoundField HeaderText="Address" DataField="address" SortExpression="address" />
          <asp:BoundField HeaderText="City" DataField="city" SortExpression="city" />
          <asp:BoundField HeaderText="State" DataField="state" SortExpression="state" />
          <asp:BoundField HeaderText="Zip Code" DataField="zip" SortExpression="zip" />
          <asp:CheckBoxField HeaderText="Contract" SortExpression="contract" DataField="contract" />
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone], [address], [city], [state], [zip], [contract] FROM [authors]"
        UpdateCommand="UPDATE [authors] SET [au_lname] = @au_lname, [au_fname] = @au_fname, [phone] = @phone, [address] = @address, [city] = @city, [state] = @state, [zip] = @zip, [contract] = @contract WHERE [au_id] = @au_id"
        ConnectionString="<%$ ConnectionStrings:Pubs %>" />
    </form>
  </body>
</html>

Open in new window

0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22870715
This question has been answered:
http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_2.0/Q_23864235.html

meeran I think you should request to delete this question.

JINN
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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