GridView DataKeyName for a DropDownList Select Statement - c#

When the user selects a row to edit I have a dropdownlist as one of the controls. In order for me to populate that ddl I need one of the datakeyname values (there are three). I was guessing that I could retrieve this value when the OnEditing event fired and pass it to the select statement for the ddl. Just not sure how to do this. I am using a stored procedure to query the Database.
This is my sqldatasource for the ddl -
<asp:SqlDataSource ID="SqlDataSourceDebtor" runat="server"
ConnectionString="<%$ ConnectionStrings:AuditDevConnectionString2 %>"
SelectCommand="sp_fc_vm_getDebtorList" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="0" Name="ClientKey"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
The "ClientKey" is the datakeyname value I need.

There are multiple approach to do this, please check http://weblogs.asp.net/aghausman/archive/2009/01/08/get-primary-key-on-row-command-gridview.aspx
Let me know if you want anything specific other than this.

I did a post on this a while back here: http://peterkellner.net/2006/10/14/showallingridviewfromddl/

Related

The server tag is not well formed asp.net C#

I'm doing the following query, and trying to use a code that come from a previous query.
But is giving me the following error: The server tag is not well formed.
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:TesteConnectionString %>" SelectCommand="SELECT * FROM [Equipa] where idAssemb=1 and idDept=<%# Eval("idDept") %>"></asp:SqlDataSource>
I'm using C# in Web forms asp.net
Can somebody help me on this?
Eval is used in DataBound controls to evaluate a field value in a row from the data source. You are trying to use it inside a Data Source control itself (SQLDataSource in this case). You should use parameterized query by specifying the value of parameter inside SelectParameters tag like this:-
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:TesteConnectionString %>"
SelectCommand="SELECT * FROM [Equipa] where idAssemb=1 AND idDept=#DeptId>
<SelectParameters>
<asp:ControlParameter ControlID="lblDeptId" Name="DeptId"
PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Please note, here I have shown the example of a control present inside your WebForm. You can specify where the value of DeptId is coming from via Cookie, QueryString, Form, Session etc.

Initialize a GridView with an initially unused SelectParameter

Is there a way to define SessionParamters in the SelectParameters of a DataSource that aren't called in the SelectCommand?
I want the parameter to be defined so it can be used later, after initial page generation, but when it's not used in the SelectCommand the DataSource doesn't seem to work - the GridView which calls it appears empty, yet generates fine when the unused Parameters are excluded.
For example, the following definition will fill the GridView successfully:
<asp:SqlDataSource ID="DataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" SelectCommand="SELECT * FROM Customers WHERE #UsedParameter='Green'">
<SelectParameters>
<asp:SessionParameter Name="UsedParameter" SessionField="Parameter1" />
</SelectParameters>
</asp:SqlDataSource>
Whereas the following would not fill the GridView:
<asp:SqlDataSource ID="DataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" SelectCommand="SELECT * FROM Customers WHERE #UsedParameter='Green'">
<SelectParameters>
<asp:SessionParameter Name="UsedParameter" SessionField="Parameter1" />
<asp:SessionParameter Name="UnUsedParameter2" SessionField="Parameter2" />
</SelectParameters>
</asp:SqlDataSource>
That is interesting... I think I've seen that problem before when I forgot to remove a parameter. Since you are likely going to change the select command later to use the other parameter, why don't you just add the parameter at that time?
VB
Dim p As New SessionParameter("UnUsedParameter2", "Parameter2")
DataSource1.SelectParameters.Add(p)
C#
SessionParameter p = new SessionParameter("UnUsedParameter2", "Parameter2")
DataSource1.SelectParameters.Add(p);

C# ASP.NET SQL Data Source

How would I use a SQL Data Source to return the value from the select statement into a variable?
<asp:SqlDataSource ID="sdsProfile" runat="server" ConnectionString="<%$ ConnectionStrings:MYDB %>" SelectCommand="SELECT * FROM [UserProfileInformation] WHERE ([UserID] = #UserID)" >
<SelectParameters>
<asp:Parameter Direction="Output" Name="Verified" Type="Int32" />
</SelectParameters>
I have a colum that is called Verified in the database that is a bit. It is either a 0 or a 1. This tells me if the user profile has been verified by the admin. Is there a way to take the return value from the select statement and put it into the Verified parameter? This way I can access that parameter value in the code behind and manipulate the page based on a 0 or a 1? Thanks!
I am pretty sure you cannot do it without code-behind. At least not the way you can respect yourself the next day.

Formview EditItemTemplate in asp.net c#

I have a product table that am updating. the fields updated are category_id, prod_name, prod_desc, prize, status. I am using a formview to do that updating. however i am having problems with the category_id. the category_id is a databound dropdownlist using a sqldatasource to fetch id and name from category table in the database. When a product has no category_id value, a null value is entered by default by sql.
The problem comes when i go to edititemtemplate mode in formview, the dropdownlist cannot show a null value from db so it throws an exception of:
'category_idDropdown' has a SelectedValue which is invalid because it
does not exist in the list of items. Parameter name: value
this is my code in the updateprod.aspx page:
<asp:DropDownList ID="category_idDropdown" runat="server"
AppendDataBoundItems="True"
AutoPostBack="True"
DataSourceID="catnames"
DataTextField="category_name"
DataValueField="category_id" ViewStateMode="Enabled"
SelectedValue='<%# Bind("category_id") %>' >
<asp:ListItem Text="-- Choose Category--" Value="0" Selected="True">
</asp:ListItem>
</asp:DropDownList>
this is my sqldatasource code the dropdownlist is databound to:
<asp:SqlDataSource ID="catnames" runat="server"
ConnectionString="<%$ ConnectionStrings:cloud_Kewl.Properties.Settings.conString %>"
SelectCommand="SELECT [category_id], [category_name] FROM [category]">
</asp:SqlDataSource>
& finally this is the sqldatasource used to update the product details:
<asp:SqlDataSource ID="productUpdate" runat="server"
ConnectionString="<%$ ConnectionStrings:cloud_Kewl.Properties.Settings.conString %>"
SelectCommand="SELECT prod_id, category_id, prod_name, prod_desc, prod_price, img_name, img_contenttype, prod_img, status FROM product WHERE (prod_id = #prod_id)"
UpdateCommand="UPDATE product SET category_id = #category_id, prod_name = #prod_name, prod_desc = #prod_desc, prod_price = #prod_price, status = #status WHERE (prod_id = #prod_id)">
<SelectParameters>
<asp:QueryStringParameter Name="prod_id" QueryStringField="prod_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="category_id" Type="Int32" DefaultValue="0" />
<asp:Parameter Name="prod_name" Type="String" />
<asp:Parameter Name="prod_desc" Type="String" />
<asp:Parameter Name="prod_price" Type="Decimal" />
<asp:Parameter Name="status" Type="String" />
<asp:Parameter Name="prod_id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
i looked up in the questions regarding the formview and edititemtemplate in this site, but none came close to give me any idea on how to approach this problem.
I tried to solve it programatically but not success in cracking this problem. Please help me, i looked for the solution for 3 weeks. no success :(
Try rebinding your datasource prior to the null value being set:
protected void myFormView_OnItemEditing(object sender, ListViewEditEventArgs e)
{
DropDownList category_idDropdown = (DropDownList)(myFormView.FindControl("category_idDropDown"));
category_idDropdown.DataBind();
category_idDropdown.Items.Add(0, new ListItem("Select", null));
}
The problem likely isn't that you're trying to select a value that doesn't exist, but rather that you're trying to bind a DropDownList that already has a null value to a datasource that doesn't contain that value.

Multiple possible FilterExpression's on SqlDataSource issue

I have an SqlDataSource in a page, which will display different results depending on 1 or 2 query strings, and/or a filter textbox.
Here is my SqlDataSource and filters:
<asp:SqlDataSource ID="sdsAudits" runat="server"
ConnectionString="<%$ ConnectionStrings:constring %>"
SelectCommand="SELECT * FROM [Audit]" FilterExpression="source = {0} AND customer = {1} AND (itemID like '%{2}%' OR parentID like '%{2}%')">
<FilterParameters>
<asp:QueryStringParameter Name="source" QueryStringField="source" />
<asp:QueryStringParameter Name="customer" QueryStringField="customer" />
<asp:ControlParameter Name="txtFilter" ControlID="txtFilter" PropertyName="Text" />
</FilterParameters>
But for some reason, the filtering for any of the 3 possible filters don't work. I tried taking out the 2 query string filters, and leaving just the textbox filter, and it worked fine then - so I'm guessing my filter expression is wrong?
Any ideas guys? Remember all 3 filters could be 'active' at once, if they have the 2 query strings and have typed into the textbox, or they could be none at all, or of course anything in between.
Filter is intended to work in DataSet mode - data is retrieved into memory, and then filtered. Not very efficient - much better to use parameters in the select command itself, like so:
<asp:SqlDataSource ID="sdsAudits" runat="server"
ConnectionString="<%$ ConnectionStrings:constring %>"
SelectCommand="SELECT * FROM [Audit] where source = #source AND customer = #customer AND (itemID like '%' + #txtFilter + '%' OR parentID like '%'+#txtFilter+'%')">
<SelectParameters>
<asp:QueryStringParameter Name="source" QueryStringField="source" />
<asp:QueryStringParameter Name="customer" QueryStringField="customer" />
<asp:ControlParameter Name="txtFilter" ControlID="txtFilter" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
or just add
DataSourceMode="DataSet"
to your SqlDataSource tag

Categories