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.
Related
I want to modify items in gridview by update command but in same page i have also a textbox with required validation.
i'm unable to update in gridview when require validation occur in textbox Control
And Source Code here...
<div class="row">
<asp:Button ID="Button1" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="Button1_Click"
ValidationGroup="btn" />
<asp:Button ID="Button2" runat="server" CssClass="btn btn-primary" Text="Reset" OnClick="Button2_Click"
CausesValidation="False" />
<asp:Button ID="Button3" runat="server" CssClass="btn btn-primary" Text="Show" CausesValidation="False"
OnClick="Button3_Click" />
</div>
</form> </div> </div>
<asp:GridView ID="GridView1" runat="server" CssClass="table-hover table-responsive table-condensed table-bordered"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1" Visible="False">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="Loc" HeaderText="Location" SortExpression="Loc" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mycon %>"
SelectCommand="SELECT * FROM [location]" DeleteCommand="Delete From Location Where Id=#id"
UpdateCommand="update location set loc=#loc where id=#id">
<DeleteParameters>
<asp:ControlParameter ControlID="TextBox1" Name="id" PropertyName="Text" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="loc" />
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue" />
</UpdateParameters>
</asp:SqlDataSource>
and cs code here
protected void Button1_Click(object sender, EventArgs e)
{
string qry = "insert into location (loc)values(#loc) ";
SqlConnection con = Connection.Getconnection();
con.Open();
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("#loc", TextBox1.Text);
int x = cmd.ExecuteNonQuery();
if (x > 0)
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation!", "<script>alert('Successfully Add')</script>");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation!", "<script>alert('Error')</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
}
protected void Button3_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
}
Please try to add try catch for button1_click to catch Exceptions.
Validation group has an effect only when the value of the CausesValidation property is set to true.and validation group need to specify a value
Please Refer this Link
I am making a course registration system in asp.net. Now i am building a page that shows the transcript of the student, i.e. all the courses he/she has taken. To do this, i use two table: courses table which keeps the records of all courses, and registration table where i keep the records of all registrations. I click a link button called lbShowTranscript which redirects me to StudenGetTranscript page. I have a gridview in that newly redirected page to show transcript, bu it gives no results, it is always empty. Here is the code:
public partial class StudentGetTranscript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int getid = MyGlobals.student.getID();
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CourseCode,CourseNumber,Term,StudentID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CourseCode" HeaderText="CourseCode" ReadOnly="True"
SortExpression="CourseCode" />
<asp:BoundField DataField="CourseNumber" HeaderText="CourseNumber"
ReadOnly="True" SortExpression="CourseNumber" />
<asp:BoundField DataField="Term" HeaderText="Term" ReadOnly="True"
SortExpression="Term" />
<asp:BoundField DataField="StudentID" HeaderText="StudentID" ReadOnly="True"
SortExpression="StudentID" />
<asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
<asp:BoundField DataField="CourseName" HeaderText="CourseName"
SortExpression="CourseName" />
<asp:BoundField DataField="CRN" HeaderText="CRN" SortExpression="CRN" />
<asp:BoundField DataField="Credit" HeaderText="Credit"
SortExpression="Credit" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT RegisterTable.CourseCode, RegisterTable.CourseNumber, RegisterTable.Term, RegisterTable.StudentID, RegisterTable.Grade, CourseTable.CourseName, CourseTable.CRN, CourseTable.Credit FROM RegisterTable INNER JOIN CourseTable ON RegisterTable.CourseCode = CourseTable.CourseCode WHERE (RegisterTable.StudentID = #getid)">
<SelectParameters>
<asp:Parameter Name="getid" />
</SelectParameters>
</asp:SqlDataSource>
Can anyone help me with this? Thanks
In your page load, you need to set the select parameter value:
SqlDataSource1.SelectParameters["getid"].DefaultValue = getid;
Another way to set the value is:
SqlDataSource1.SelectParameters.Add("getid", getid);
This code does nothing:
protected void Page_Load(object sender, EventArgs e)
{
int getid = MyGlobals.student.getID();
}
As soon as the function ends, getid disappears. You need to shove that value into your select parameter.
I have a gridview 'gvAccounts' with a column 'Entitlements' (amongst others). Each Entitlements cell contains another gridview 'gvEntitlements', with a list of entitlements for each account (each row in gvAccounts is an account).
In the EditTemplate for the Entitlements cell, I have a CheckBoxList 'cblEntitlements', which is filled with all of the available entitlements.
When I click Edit, I want the cblEntitlements to only have the those entitlements checked which appear in gvEntitlements. I tried a bunch of things in the RowEditing event and nothing worked (lots of null reference errors when using .FindControl on the row), so I thought I would just ask what the best way to do this is, rather than paste my failed attempts.
Thanks!
Markup:
<asp:GridView
ID="gvAccounts"
runat="server"
DataSourceID="AccountsObjectDataSource"
AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="CorpID,AppKey"
Width="100%"
OnRowDataBound="gvAccounts_RowDataBound"
OnRowEditing="gvAccounts_RowEditing"
EnableModelValidation="True" >
<Columns>
<asp:TemplateField
HeaderText="Entitlements"
SortExpression="Entitlements">
<ItemTemplate>
<asp:GridView
ID="gvEntitlements"
DataKeyNames="EntitlementID"
runat="server"
ShowHeader="False"
BorderStyle="None"
EmptyDataText="common"
GridLines="None"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField
DataField="EntitlementID"
Visible="false" />
<asp:BoundField
DataField="Entitlement"/>
</Columns>
</asp:GridView>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList
ID="cblEntitlements"
runat="server"
DataSourceID="cblObjectDataSource"
DataTextField="Entitlement"
DataValueField="EntitlementID"
RepeatColumns="2"
RepeatDirection="Horizontal">
</asp:CheckBoxList>
<asp:ObjectDataSource
ID="cblObjectDataSource"
runat="server"
SelectMethod="GetAppEntitlements"
TypeName="CMRPWebApp.Business.CMRPControllerApps">
<SelectParameters>
<asp:ControlParameter
ControlID="DropDownList1"
Name="AppKey"
PropertyName="SelectedValue"
Type="Int32"/>
</SelectParameters>
</asp:ObjectDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource
ID="AccountsObjectDataSource"
runat="server"
SelectMethod="GetUsersForApp"
TypeName="CMRPWebApp.Business.CMRPControllerApps"
<SelectParameters>
<asp:ControlParameter
ControlID="DropDownList1"
Name="AppKey"
PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
code-behind:
protected void gvAccounts_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//for each row, run a query for that appkey + corp ID to retrieve the entitlements for that account.
System.Collections.Specialized.IOrderedDictionary datakeyNames = gvAccountsApp.DataKeys[e.Row.RowIndex].Values;
string corpID = datakeyNames["CorpID"].ToString();
int appKey = Convert.ToInt32(datakeyNames["AppKey"]);
if ((e.Row.FindControl("gvEntitlements") as GridView) != null)
{
GridView gv = e.Row.FindControl("gvEntitlements") as GridView;
gv.DataSource = CMRPControllerApps.GetAccountEntitlements(corpID, appKey);
gv.DataBind();
}
}
}
protected void gvAccounts_RowEditing(object sender, GridViewEditEventArgs e)
{
// ?
}
From my understanding of your question, you want the edit template to show all entitlements, with only the entitlements that belong to the specific account being checked.
A couple of things that I noticed:
In your code behind, it looks like you are binding something named GridView1 to the list of entitlements for a specific account. I don't see anything named that in your code.
Are you binding to the gridview twice? In your markup I see that you are setting the datasource to cblEntitlements. Is this the specific problem you are facing?
I apologize if I am missing the point here, just a little confused.
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 have used a gridview and linqdatasourse , the gridview will be fill by a linq-stored procedure after clicking a search button.
I did it like below but my grid view is empty.
It seems LinqDataSource2_Selecting does not work.
Please help what is the problem?
public void LinqDataSource2_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
_DataContext = new EDMSDataContext();
var subjectFilter = e.WhereParameters["Subject"];
var query = _DataContext.spQuickSearchDoc(subjectFilter);
e.Result = query;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
_DataContext = new EDMSDataContext();
this.LinqDataSource2.WhereParameters["Subject"].DefaultValue = this.txtSearchKeywords.Text;
GridViewDocuments.Visible = false;
GridViewDocuments_Search.Visible = true;
this.GridViewDocuments_Search.DataBind();
}
Stored procedure:
ALTER PROCEDURE [dbo].[spQuickSearchDoc]
#Searchtext varchar(50)=null
AS
select DocId,DocumentNo,Title,Unit
from tblDocuments
where DocumentNo like '%'+#SearchText + '%'
or Title like '%'+#SearchText + '%'
or Unit like '%'+#SearchText + '%'
Gridview:
<asp:GridView ID="GridViewDocuments_Search" runat="server" AutoGenerateColumns=False Visible="False" onrowcommand="GridViewDocuments_Search_RowCommand"
DataKeyNames="DocID" PageSize="100" >
<Columns>
<asp:TemplateField HeaderText = "Details">
<ItemTemplate>
<asp:Button ID ="btn_Show" Text="Details" runat= "server" CommandName= "Details" CommandArgument='<%#
Container.DataItemIndex%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DocumentNo" HeaderText="DocumentNo" SortExpression="DocumentNo" />
<asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
<asp:BoundField DataField="Docid" HeaderText="Docid" Visible="false" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource2" runat="server"
ContextTypeName="EDMSDataContext" OnSelecting="LinqDataSource2_Selecting">
<WhereParameters>
<asp:ControlParameter Name="Subject"
ControlID="txtSearchKeywords"
PropertyName="Text"
Type="String" />
</WhereParameters>
</asp:LinqDataSource>
You need to add DataSourceID to the GridView, as in:
<asp:GridView ID="GridViewDocuments_Search" runat="server"
. . DataSourceID="LinqDataSource2">
Since it's not set, the GridView is being bound to NULL.