I am trying to pass a variable to a SelectCommand in SqlDataSource. I have this MyIdVal that need to be passed.
Here is the code :
<form id="form1" runat="server">
<div>
<%=MyIdVal%>
</div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="myIdDataSource">
</asp:GridView>
<asp:SqlDataSource runat="server" ID="myIdDataSource"
ConnectionString="<%$ ConnectionStrings:myCipConnection %>"
ProviderName="<%$ ConnectionStrings:myCipConnection.ProviderName %>"
SelectCommand="SELECT * FROM books WHERE id = #MyIdVal" >
</asp:SqlDataSource>
</form>
The code works fine if I hardcode the id, so how this works?
Here's two ways to do it.
Bind the Parameter value to a control
<selectparameters>
<asp:controlparameter
name="MyIdVal"
controlid="DropDownListBooks"
propertyname="SelectedValue"/>
</selectparameters>
2 . Use the SqlDataSource.Selecting Event
protected void myIdDataSource_Selecting
(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["MyIdVale"] = MyIdVal;
}
Related
i have two gridviews on a webform in asp.net with c# code behind. i also have 3 tables: user, group and usergroup.
one gridview contains a list of groups with two columns: description and a buttonfield. when the user clicks on this buttonfield, the members of the selected group should be displayed in the second gridview.
however, i get an error "must declare scalar variable #GruppenID every time i click the buttonfield. what am i missing here? sorry but i am completely new to asp and sql...
WORKING:
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Gruppenverwaltung.aspx.cs" Inherits="WerIstWo.Gruppenverwaltung" %>
<asp:Content ID="content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h1>Gruppenverwaltung</h1>
<asp:Panel ID="pnlGruppe" ScrollBars="Both" runat="server">
<asp:Button ID="btnNeueGruppe" Text="Neue Gruppe" runat="server" OnClick="btnNeueGruppe_Click" />
<asp:GridView DataKeyNames="GruppenID" OnRowCommand="grdGruppe_RowCommand" ID="grdGruppe" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Bezeichnung" HeaderText="Bezeichnung" SortExpression="Bezeichnung" />
<asp:TemplateField HeaderText="Mitglieder anzeigen">
<ItemTemplate>
<asp:Button ID="btnMitgliederAnzeigen" runat="server" Text="Mitglieder anzeigen" CommandName="MitgliederAnzeigen"
CommandArgument='<%# Eval("GruppenID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Archivieren" ButtonType="Button" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
<asp:SqlDataSource OnSelected="SqlDataSource1_Selected" ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Bezeichnung], [GruppenID] FROM [Gruppe] WHERE [Archiviert] != 1"
DeleteCommand="UPDATE Gruppe SET [Archiviert] = 1 WHERE [GruppenID] = #GruppenID">
</asp:SqlDataSource>
<asp:Button ID="btnZurueck" Text="Zurück" runat="server" OnClick="btnZurueck_Click" />
</asp:Panel>
<asp:Panel Visible="false" ID="pnlMitglieder" ScrollBars="Both" runat="server">
<asp:GridView ID="grdBenutzer" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="Vorname" HeaderText="Vorname" SortExpression="Vorname" />
<asp:BoundField DataField="Nachname" HeaderText="Nachname" SortExpression="Nachname" />
<asp:BoundField DataField="Geburtsdatum" HeaderText="Geburtsdatum" SortExpression="Geburtsdatum" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT a.Vorname,
a.Nachname,
a.Geburtsdatum
FROM [Benutzer] a
INNER JOIN [BenutzerGruppe] b
ON a.BenutzerID = b.BenutzerID
INNER JOIN [Gruppe] c
ON b.GruppenID = c.GruppenID
WHERE c.GruppenID = #GruppenID">
<SelectParameters>
<asp:Parameter Name="GruppenID" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" Text="Zurück" runat="server" OnClick="Button1_Click" />
</asp:Panel>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace WerIstWo
{
public partial class Gruppenverwaltung : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserAuthentication"] == null)
{
Response.Redirect("Login.aspx");
}
}
protected void btnZurueck_Click(object sender, EventArgs e)
{
Response.Redirect("Datenverwaltung.aspx");
}
protected void btnNeueGruppe_Click(object sender, EventArgs e)
{
Response.Redirect("NeueGruppe.aspx");
}
protected void grdGruppe_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MitgliederAnzeigen")
{
string index = e.CommandArgument.ToString();
pnlMitglieder.Visible = true;
pnlGruppe.Visible = false;
SqlDataSource2.SelectParameters["GruppenID"].DefaultValue = index;
grdBenutzer.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
pnlMitglieder.Visible = false;
pnlGruppe.Visible = true;
}
protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
}
}
}
You must pass SqlDataSource Select Parameter.
As you are new a whole process should be like this as your need
add CommandArgument property in button Field
// <asp:ButtonField ButtonType="Button" CommandArgument="GruppenID" CommandName="MitgliederAnzeigen" Text="Mitglieder anzeigen" />
replace this to
<asp:TemplateField HeaderText="Mitglieder anzeigen">
<ItemTemplate>
<asp:Button ID="btn1" runat="server" Text="Mitglieder anzeigen" CommandName="MitgliederAnzeigen"
CommandArgument='<%# Eval("GruppenID") %>' />
</ItemTemplate>
</asp:TemplateField>
your SqlDataSource2 must be
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="select a.Nachname,
c.GruppenID
FROM [Benutzer] a
INNER JOIN [BenutzerGruppe] b
ON a.BenutzerID = b.BenutzerID
INNER JOIN [Gruppe] c
ON b.GruppenID = c.GruppenID
WHERE c.GruppenID = #GruppenID
">
<SelectParameters>
<asp:Parameter Name="GruppenID" />
</SelectParameters>
</asp:SqlDataSource>
now on row command
if (e.CommandName == "MitgliederAnzeigen")
{
string index = e.CommandArgument.ToString();
pnlMitglieder.Visible = true;
pnlGruppe.Visible = false;
SqlDataSource2.SelectParameters["GruppenID"].DefaultValue = index;
grdBenutzer.DataBind();
}
You need to declare the SQL parameter #GruppenID in SqlDataSource1 for both the SelectCommand and the DeleteCommand, like this:
<asp:SqlDataSource OnSelected="SqlDataSource1_Selected" ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Bezeichnung], [GruppenID] FROM [Gruppe] WHERE [Archiviert] != 1"
DeleteCommand="UPDATE Gruppe SET [Archiviert] = 1 WHERE [GruppenID] = #GruppenID">
<SelectParameters>
<asp:Parameter Name="GruppenID" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="GruppenID" />
</DeleteParameters>
</asp:SqlDataSource>
You can then set the values for this parameter like in this thread.
I am trying to insert names (string) in to a test database (sqlce). Basically, user will type in name in the textbox (input_box) and click on "Insert Button". Upon click of the button, a call is made to "submit" function. I want the text from the textbox to be used as the parameter for insertcommand. I don't get any error while running it. But when I check the data in the table, only "null" is inserted. I am trying to learn asp.net. Any help would be appreciated. Thank you.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"><title>Test run</title></head>
<script language="C#" runat = "server">
void Submit(object o, EventArgs e)
{
SqlDataSource1.Insert();
}
</script>
<body style="height: 395px">
<form id="form1" runat="server">
<asp:TextBox ID="Input_box" runat="server"></asp:TextBox>
<asp:Button ID="Insert_button" runat="server" Text=" Insert Button" OnClick = "Submit"/>
<asp:SqlDataSource
ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
InsertCommand="INSERT INTO Incident_Table(Assignee) VALUES (#assignee)"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT Assignee FROM Incident_Table">
<InsertParameters>
<asp:FormParameter Name="assignee" FormField ="name"/>
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
you should replace this:
<asp:FormParameter Name="assignee" FormField ="name"/>
with:
<asp:FormParameter Name="assignee" FormField="Input_box" />
I have a main 'Products' table and a 'Products_Recommended' table. I want the users to be able to select several products from a GridView using checkboxes and then insert those Product IDs (prodid) into the Products_Recommended table in such a way that a main Product ID is entered (coming from query string) and potentially several recommended ProdIDs get entered. So far so good.
But I need to be able to show the checkboxes to be checked if there were already prodids in the Products_Recommended table previously.
The code below show a 'sqldatasource1' which gets the data from the Products_Recommended table based on query string. I just don't know how the checkboxes can get checked because the GridView has a different sqldatasource binding it.
Thanks!
Meengla
<form id="form1" runat="server">
<asp:GridView ID="Products" runat="server" AutoGenerateColumns="False" DataKeyNames="prodid"
DataSourceID="alldata" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ProductSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="itemnumber" HeaderText="Item Number" SortExpression="itemnumber" />
<asp:BoundField DataField="itemtitle" HeaderText="itemtitle" SortExpression="itemtitle" />
</Columns>
</asp:GridView>
<p>
<asp:Button ID="SelectedProducts" runat="server" Text="Recommend" OnClick="SelectedProducts_Click" />
</p>
<p>
<asp:Label ID="lblProdSelected" runat="server" EnableViewState="False" Visible="False"></asp:Label>
</p>
<asp:SqlDataSource ID="alldata" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
SelectCommand="SELECT * FROM Products">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
SelectCommand="SELECT * FROM dbo.products_recommended WHERE prodid = #itemid)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Handle the RowDataBound event, and in the method find the checkbox and set its Checked value.
<asp:GridView ID="Products" OnRowDataBound="GridViewRowEventHandler">
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var ProductSelector = e.Row.FindControl("ProductSelector") as CheckBox;
ProductSelector.Checked = true;
}
}
You can use the Select method of DataSource to retrieve the data you need
Using Visual Web Developer Express 2010 with ASP.NET 4.0.
I have a FormView and I want to set a default value from the database. I can't get it to bind to the value in the database. My FormView looks like this:
<asp:FormView
ID="frmOrderDetails"
DataSourceID="sdsFormOrderDetails"
runat="server"
DataKeyNames="orderId">
<EditItemTemplate>
<h3>Edit Order Details</h3>
<asp:Label ID="lblStrategy" Text="Strategy:" AssociatedControlID="ddlStrategies" runat="server" />
<asp:DropDownList SelectedValue='<%# Bind("strategyId") %>'
ID="ddlStrategies"
runat="server"
DataTextField="strategy"
DataValueField="strategyId"
DataSourceID="sdsStrategies"
/>
<asp:LinkButton
id="lnkUpdate"
Text="Update Order"
CommandName="Update"
Runat="server" />
|
<asp:LinkButton
id="lnkCancel"
Text="Cancel"
CommandName="Cancel"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sdsFormOrderDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetOrderDetails" SelectCommandType="StoredProcedure"
UpdateCommand="usp_UpdateOrder" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsStrategies" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetStrategiesDropDown">
</asp:SqlDataSource>
The EditItemTemplate does not even load when I click the edit button on my FormView control, but I don't get an error message either.
You need to do the following to bind a DropDownList to the EditItemTemplate:
Add the DropDownList and its DataSource to the EditItemTemplate.
Set the DropDownList parameters:
DataSourceID="SqlDataSourceDropDownlist" SelectedValue=<%# Bind("ValueToBind") %>
DataTextField="ValueToDisplay" DataValueField="ValueToBind"
Set the ListView / FormView DataSource <UdateParameters>: <asp:Parameter Name="RankID" Type="Int32" />
you need to use formview Databound event like
protected void frmOrderDetails_DataBound(object sender, EventArgs e)
{
if (frmOrderDetails.CurrentMode == FormViewMode.Edit)
{
DropDownList ddlStrategies = (DropDownList)frmOrderDetails.FindControl("ddlStrategies");
ddlStrategies.SelectedValue = Your DB Value Goes here;
}
}
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FlowServerConnectionString %>"
SelectCommand="SELECT [RepGroup_Name], [RepGroup_ID] FROM [Rep_Group] WHERE [RepGroup_ID] in
(SELECT [ID_RepGroup]
FROM [FlowServer].[dbo].[Rep_Permission]
WHERE [ID_User]=#ID_User)">
<SelectParameters>
<asp:ProfileParameter DefaultValue="" Name="ID_User" PropertyName="UserName" />
</SelectParameters>
How to insert System.Web.HttpContext.Current.User.Identity.Name to my property "ID_User" (it's username here)
On your SqlDataSource1 add OnInit="SqlDataSource1_Init" then add the following event to the code behind:
protected void SqlDataSource1_Init(object sender, EventArgs e)
{
this.SqlDataSource1.SelectParameters["ID_User"].DefaultValue = System.Web.HttpContext.Current.User.Identity.Name;
}