System.Web.HttpContext.Current.User.Identity.Name as SqlDataSource property - asp.net

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

Related

using full text search with a sqldatasource

I'm having issues with using a full text search with an sqldatasource.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT 1, 2, 3 FROM table WHERE CONTAINS(2, #text)">
<SelectParameters>
<asp:ControlParameter ControlID="tbOCRSearch" Name="text"
Type="String" DefaultValue="" PropertyName="Text" />
</SelectParameters>
This works great if I only put in one search term. However if I put in two terms it throws an error.
However if I put "termone termtwo" in double quotes the query works.
how can I modify my selectcommand to add the doublequotes?
Solved.
Took me a few hours of looking. In the sqldatasource we need to add a OnSelecting event.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT 1, 2, 3 FROM table WHERE CONTAINS(2, #text)"
OnSelecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:ControlParameter ControlID="tbOCRSearch" Name="text"
Type="String" DefaultValue="" PropertyName="Text" />
</SelectParameters>
Then in the code behind you'll need one of these. You can then add the quotes around the text.
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["#text"].Value = "\"" + tbOCRSearch.Text + "\"";
}
And now everything works like it should.

ASP.NET GridView passing parameter

I am not sure what I am doing wrong but no value is being returned on my grid.
I have the following in my .aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
SqlDS1.SelectParameters.Add("#ID", "6");
}
I have the following in my .aspx file
<asp:SqlDataSource ID="SqlDS1" runat="server" ConnectionString="<%$ ConnectionStrings:LisSQL %>"
SelectCommand="select RemediationID, RemediationDate, RemediationUser, RemediationAction from VAPHS_Remediation WHERE ID = #ID">
<SelectParameters>
<asp:Parameter Name="ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDS1" Width="1200px" AutoGenerateColumns="False" AllowSorting="True">
<Columns>
<asp:BoundField DataField="RemediationID" HeaderText="RemediationID"/>
<asp:BoundField DataField="RemediationDate" HeaderText="RemediationDate"/>
<asp:BoundField DataField="RemediationUser" HeaderText="RemediationUser"/>
<asp:BoundField DataField="RemediationAction" HeaderText="RemediationAction"/>
</Columns>
</asp:GridView>
Nothing gets returned although there is a record for ID = 6
You want to use DefaultValue instead of add.
protected void Page_Load(object sender, EventArgs e)
{
SqlDS1.SelectParameters["ID"].DefaultValue = "6";
}
You already have declarative parameter already; if you use Add parameter, it'll add another duplicate one.

"must declare scalar variable" when using gridview

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.

SqlDataSource pass variable

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

ASP.NET Could not find control in ControlParamater

I'm pretty lost at this point (been working at it for a while not and am hitting a wall / deadline) but the error message I am being thrown is after I hit btnupdate to update the fields in the database.
Full Error Message:
Could not find control 'txtTitle' in ControlParameter 'Title'.
Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="floater">
<h3>Books</h3>
<asp:DropDownList
id="DropDownList_Books"
DataSourceId="srcBooks"
DataTextField="Title"
DataValueField="Id"
Runat="server" />
<asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px"
onclick="btnSelect_Click" />
</div>
<asp:GridView
id="grdBooks"
DataSourceID="srcBooks_Description"
Runat="server" Visible="False" />
<asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />
</div>
<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click"
Text="Cancel" Visible="False" />
<asp:FormView
id="frmEditBook"
DataKeyNames="Cat_Id"
DataSourceId="srcBooks_Description"
DefaultMode="Edit"
Runat="server" Visible="False"
style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px" >
<EditItemTemplate>
<asp:Label
id="lblTitle"
Text="Title:"
AssociatedControlID="txtTitle"
Runat="server" />
<asp:TextBox
id="txtTitle"
Text='<%#Bind("Title")%>'
Runat="server" />
<br />
<asp:Label
id="lblDescription"
Text="Description:"
AssociatedControlID="txtDescription"
Runat="server" />
<asp:TextBox
id="txtDescription"
Text='<%#Bind("Description")%>'
Runat="server" />
<br />
<asp:Button
id="btnUpdate"
Text="Update"
CommandName="Update"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="srcBooks" runat="server"
ConnectionString="Data Source=****;;Initial Catalog=***;Persist Security Info=True;User ID=****;Password=****"
onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="srcBooks_Description" runat="server"
ConnectionString="Data Source=****, 14330";Initial Catalog=****;Persist Security Info=True;User ID=****;Password=****"
onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE Cat_ID=#Id" UpdateCommand="UPDATE PARTIN_INFO SET Title=#Title,
Description=#Description WHERE Cat_Id=#Id">
<SelectParameters>
<asp:ControlParameter
Name="Id"
Type="int32"
ControlID="DropDownList_Books"
PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="Title" ControlId="txtTitle" PropertyName="Text"/>
<asp:ControlParameter Name="Description" ControlId="txtDescription" PropertyName="Text"/>
<asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
Code Behind:
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void srcBooks_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void btnSelect_Click(object sender, EventArgs e)
{
grdBooks.Visible = true;
}
protected void btnCancel_Click(object sender, EventArgs e)
{
frmEditBook.Visible = false;
}
protected void btnEdit_Click(object sender, EventArgs e)
{
frmEditBook.Visible = true;
btnCancel.Visible = true;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
srcBooks_Description.Update();
}
catch (Exception except)
{
// Handle the Exception.
}
}
}
}
You -can- refer to the control adequately.
You need to prefix the controlid value of your update parameters with the control id of your enclosing view (FormView 'frmEditBook' in this case)
Replace the UpdateParamters section of your SqlDataSource with the following:
<UpdateParameters>
<asp:ControlParameter Name="Title" ControlId="frmEditBook$txtTitle" PropertyName="Text"/>
<asp:ControlParameter Name="Description" ControlId="frmEditBook$txtDescription" PropertyName="Text"/>
<asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>
Note that
ControlId="DropDownList_Books"
remains the same as this control is not within the bounds of the FormView.
I don't recommend using the 'full name' provided by the browser... particularly if you're using master pages... it could change as you rearrange your layouts.
As you can see, the problem is the context. You can also put your sqldatasource inside the formview where the control being refered to is (not properly and very limited but could work)
Let's say you have a detailsview with two dropdowns in it, and one is dependant from another. What works for me is to put the Datasource in the same context (parent control) as the control refered and this will eliminate the need to reference the full path of the control.
My two cents is that this is very reminiscent on how you must reference controls in Access Forms with subforms. :)
<asp:DetailsView runat="server">
<asp:TemplateField>
<EditTemplate>
<asp:DropDownList id="ParentDDL" Datasource="SQLDataSource1">
</asp:DropDownList>
</EditTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditTemplate>
<asp:DropDownList id="ParentDDL" Datasource="SQLDatasource2">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * from table where field1=#field">
<SelectParameters>
<asp:ControlParameter ControlID="ParentDDL" Name="field"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</EditTemplate>
</asp:TemplateField>
</asp:DetailsView>
You need to refer to your control with its full name.
You can easily find the control's full name when you open the page in the browser. Simply view source and you will see it.
The problem is that txtTitle is inside a FormView so it's not accesible in the way you're trying to do on the SqlDataSource. You can't access it direclty by ID.
Here you have an MSDN article that may help to to work with a FormView and SqlDataSource
you shouldn't have used <asp:ControlParameter.. but <asp:Parameter.. and the SqlDataSource control with the help of the bind method is intelligent enough to locate the controls to get values from for the parameters.
Update this portion of the sqlDataSource this way:
....
<UpdateParameters>
<asp:Parameter Name="Title" Type="String"/>
<asp:Parameter Name="Description" Type="String"/>
<asp:Parameter Name="Id" Type="Int32"/>
</UpdateParameters>
.....
Please note the Type attribute and supply the relevant type.
(The problem is SqlDataSource control cannot explicitly access controls defined in a databound control)

Resources