GridView not Returning Rows - c#

I have a simple GridView that uses 3 session variables; CoName that is captured when the user logs in and PurchaseOrderDate, and HeatNumber that are captured in text boxes (both of these are optional) as input parameters for a stored procedure on a SQLServer 2012 database. I have a CustomerLanding.aspx page that has a search button on it that, when clicked, redirects the user to a TestReportLanding.aspx page that currently has the GridView on it. Everything works as it should in this scenario.
I want to move the GridView to CustomerLanding.aspx so I can display the search results on the same page that has the search parameters and the search button. Eventually I want to use AJAX for the update, but for now I'm just putting the GridView in a different div. When I use this setup, the query only returns results when the optional parameters (PurchaseOrderDate and HeatNumber) are provided. My stored procedure only requires CoName for it's search, so I should get a lot more rows returned with the optional parameters left empty.
Being somewhat new to .NET, I'm hoping that I'm missing something simple here.
Here is my GridView code in CustomerLanding along with the SqlDataSource:
<div class="col-xs-12 col-lg-10">
<asp:Label ID="NoRecordsFound" runat="server" Visible ="false" Text="No records found."></asp:Label>
<asp:GridView ID="GridView1" runat="server" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" GridLines="Vertical" BackColor="White" ForeColor="Black" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="doc" HeaderText="doc" SortExpression="doc" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Heat" HeaderText="Heat" SortExpression="Heat" />
<asp:BoundField DataField="Dir" HeaderText="Dir" SortExpression="Dir" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:iiConnectionString %>" SelectCommand="sp_test_reports" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="CustomerName" SessionField="CoName" Type="String" />
<asp:SessionParameter DbType="Date" Name="Date" SessionField="PurchaseOrderDate" />
<asp:SessionParameter Name="Heat" SessionField="HeatNumber" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
And for what it's worth here is my stored procedure (I'm certain this is not the problem because it works when the DataGrid is on a seperate page:
CREATE PROCEDURE [dbo].[sp_test_reports]
-- Add the parameters for the stored procedure here
#CustomerName varchar(50),
#Date date,
#Heat varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
IF #Heat IS NULL AND #Date IS NULL
SELECT DISTINCT TOP 1000 d.doc, d.Date, dl.Heat, d.Dir
FROM documents d
INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
WHERE d.type = 2 and d.Customer = #CustomerName;
ELSE IF #Heat IS NOT NULL AND #Date IS NULL
SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
FROM documents d
INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
WHERE d.type = 2 AND d.Customer = #CustomerName AND dl.Heat = #Heat;
ELSE IF #Heat IS NULL AND #Date IS NOT NULL
SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
FROM documents d
INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
WHERE d.type = 2 AND d.Customer = #CustomerName AND d.Date = #Date;
ELSE IF #Heat IS NOT NULL AND #Date IS NOT NULL
SELECT DISTINCT(d.doc), d.Date, dl.Heat, d.Dir
FROM documents d
INNER JOIN Documents_Line_Items dl ON d.Doc = dl.Doc
WHERE d.type = 2 AND d.Customer = #CustomerName AND d.Date = #Date AND dl.Heat = #Heat;
END
And here is the code behind my search button:
Note that if I uncomment the Response.Redirect here, the GridView works like it should.
protected void TestReports_Click(object sender, EventArgs e)
{
Session["PurchaseOrderDate"] = DatePurchaseOrder.Text;
Session["HeatNumber"] = HeatText.Text;
GridView1.Visible = true;
MessageBox.Show(Session["CoName"].ToString());
MessageBox.Show(Session["HeatNumber"].ToString());
if (GridView1.Rows.Count == 0)
{
NoRecordsFound.Visible = true;
}
else
{
NoRecordsFound.Visible = false;
}
//Response.Redirect("TestReportLanding.aspx", false);
}
Thanks in advance for the help.

In your stored procedure, initialize your two optional parameters to NULL.
#CustomerName varchar(50),
#Date date = NULL,
#Heat varchar(50) = NULL
Parameters for a stored procedure aren't really "optional" unless they've been initialized to something.

Finally found an answer here:
empty gridview although the sqldatasource has values
Basically set CancelSelectOnNullParameter="false" on the SqlDatasource.

Related

gridview control not refreshing?

I have what I hope is a simple question. I have a gridview control that's bound to a sqldatasource.
First, the relevant code:
<asp:DropDownList ID="cboCustomerID" runat="server"
DataSourceID="DataSourceCustomer" DataTextField="CustomerName" DataValueField="CustomerID">
</asp:DropDownList>
<asp:DropDownList ID="cboStaffID" runat="server"
DataSourceID="DataSourceStaff" DataTextField="StaffFullName" DataValueField="StaffID">
</asp:DropDownList>
<div><gcctl:MyCheckBox ID="chkShowClosed" Text="Show Closed Jobs?" Checked="false" AutoPostBack="true" runat="server" /></div>
<div><asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" /></div>
<div id="customersearchresults" class="searchresults">
<asp:SqlDataSource id="gvJobsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:GeekCommandConnString %>"
SelectCommand="SELECT j.JobID, j.JobName, s.JobStatus, jal.[JobAssignmentsFullList]
FROM (SELECT * FROM [dbo].[Jobs] WHERE ((NULLIF(#CustomerSearch,'') IS NOT NULL AND CustomerID = #CustomerSearch) OR (NULLIF(#CustomerSearch,'') IS NULL))) j
INNER JOIN (SELECT * FROM [list].[JobStatuses] WHERE ((#ShowClosed = 0 AND IsStatusOpen = 1) OR (#ShowClosed = 1)) AND IsActive = 1) s ON j.JobStatusID = s.JobStatusID
LEFT JOIN (SELECT * FROM [dbo].[JobAssignments] WHERE ((NULLIF(#StaffSearch,'') IS NOT NULL AND StaffID = #StaffSearch) OR (NULLIF(#StaffSearch,'') IS NULL))) ja ON j.JobID = ja.JobID
LEFT JOIN [dbo].[udv_JobAssignmentsCommaDelimited] jal ON j.JobID = jal.JobID
">
<SelectParameters>
<asp:Parameter Name="CustomerSearch" Type="Int32" />
<asp:Parameter Name="StaffSearch" Type="Int32" />
<asp:Parameter Name="ShowClosed" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gvJobs" runat="server" AllowSorting="True"
AutoGenerateColumns="False"
DataKeyNames="JobID"
DataSourceID="gvJobsDataSource"
AutoGenerateDeleteButton="False"
AutoGenerateEditButton="False"
AutoGenerateSelectButton="False"
CssClass="searchresultsgrid"
AllowPaging="True" PageSize="50"
OnRowCommand="gvJobs_RowCommand"
EmptyDataText="No matching jobs on record." >
<Columns>
<asp:templatefield>
<itemtemplate>
<asp:linkbutton id="btnEdit" runat="server" CommandName="JobEdit" OnClientClick="PageForm.target ='_blank';" text="View/Edit" />
</itemtemplate>
</asp:templatefield>
<asp:BoundField DataField="JobID" HeaderText="ID" InsertVisible="false" ReadOnly="true" Visible="false" SortExpression="JobID" />
<asp:templateField HeaderText="Job Name" SortExpression="JobName">
<ItemTemplate><%# Eval("JobName") %></ItemTemplate>
</asp:templateField>
<asp:templateField HeaderText="Assigned to" SortExpression="JobAssignmentsFullList">
<ItemTemplate><%# Eval("JobAssignmentsFullList") %></ItemTemplate>
</asp:templateField>
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="DataSourceCustomer" runat="server"
ConnectionString="<%$ ConnectionStrings:GeekCommandConnString %>"
SelectCommand="SELECT NULL AS [CustomerID]
, NULL AS [CustomerName]
UNION SELECT [CustomerID]
,[CustomerName]
FROM [GeekCommand].[dbo].[Customers]
WHERE ((#ShowInactive = 0 AND IsActive = 1) OR (#ShowInactive = 1))
ORDER BY CustomerName">
<SelectParameters>
<asp:ControlParameter Name="ShowInactive" Type="Boolean" ControlID="chkCustomersShowInactive" PropertyName="Checked" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DataSourceStaff" runat="server"
ConnectionString="<%$ ConnectionStrings:GeekCommandConnString %>"
SelectCommand="SELECT NULL AS [StaffID]
, NULL AS [StaffFullName]
UNION SELECT [StaffID]
,COALESCE([FirstName], [Nickname], '') + ' ' + COALESCE([LastName], '') AS StaffFullName
FROM [GeekCommand].[dbo].[Staff]
WHERE ((#ShowInactive = 0 AND IsActive = 1) OR (#ShowInactive = 1))
ORDER BY StaffFullName">
<SelectParameters>
<asp:ControlParameter Name="ShowInactive" Type="Boolean" ControlID="chkStaffShowInactive" PropertyName="Checked" />
</SelectParameters>
</asp:SqlDataSource>
And the relevant aspx.cs code:
int? iCustomerID;
int? iStaffID;
//--------
protected void SetIDs()
{
this.iCustomerID = null;
this.iStaffID = null;
if (Request.QueryString["customerid"] != null) //new customer
{
try
{
this.iCustomerID = Convert.ToInt32(Request.QueryString["customerid"]);
}
catch { }
}
if (Request.QueryString["staffid"] != null) //new customer
{
try
{
this.iStaffID = Convert.ToInt32(Request.QueryString["staffid"]);
}
catch { }
}
if (iCustomerID != null)
{
cboCustomerID.SelectedValue = iCustomerID.ToString();
}
if (iStaffID != null)
{
cboStaffID.SelectedValue = iStaffID.ToString();
}
}
//--------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetIDs();
}
}
//--------
protected void btnSearch_Click(object sender, EventArgs e)
{
gvJobsDataSource.SelectParameters["CustomerSearch"].DefaultValue = cboCustomerID.SelectedValue.ToString();
gvJobsDataSource.SelectParameters["StaffSearch"].DefaultValue = cboStaffID.SelectedValue.ToString();
gvJobsDataSource.SelectParameters["ShowClosed"].DefaultValue = chkShowClosed.Checked.ToString();
gvJobs.DataBind();
}
When I run the following in SSMS, I get 2 rows back, as I should:
DECLARE #CustomerSearch int, #StaffSearch int, #ShowClosed bit
SELECT #CustomerSearch = 2331, #StaffSearch = '', #ShowClosed = CAST(0 AS bit)
SELECT j.JobID, j.JobName, s.JobStatus, jal.[JobAssignmentsFullList]
FROM (SELECT * FROM [dbo].[Jobs] WHERE ((NULLIF(#CustomerSearch,'') IS NOT NULL AND CustomerID = #CustomerSearch) OR (NULLIF(#CustomerSearch,'') IS NULL))) j
INNER JOIN (SELECT * FROM [list].[JobStatuses] WHERE ((#ShowClosed = 0 AND IsStatusOpen = 1) OR (#ShowClosed = 1)) AND IsActive = 1) s ON j.JobStatusID = s.JobStatusID
LEFT JOIN (SELECT * FROM [dbo].[JobAssignments] WHERE ((NULLIF(#StaffSearch,'') IS NOT NULL AND StaffID = #StaffSearch) OR (NULLIF(#StaffSearch,'') IS NULL))) ja ON j.JobID = ja.JobID
LEFT JOIN [dbo].[udv_JobAssignmentsCommaDelimited] jal ON j.JobID = jal.JobID
But when I select a customer (specifically the customer whose id is 2331) in debug mode and step through the btnSearch_Click code, cboCustomerID.SelectedValue = "2331", cboStaffID.SelectedValue = "", chkShowClosed.Checked = false (all of which is correct)... but nothing happens when I step past the databind command. The gridview continues to show "No matching jobs on record."
I feel like I'm missing something really obvious, but I can't for the life of me figure out what it is.
UPDATE: Ok. This is interesting. Apparently, the query never gets sent to the SQL Server. I just started up SQL Server in trace mode, reloaded the aspx page, and did a search, and while the queries that are behind the two dropdownlists are there in the log, the query that's behind the gridview is just not there.
UPDATE #2: I've replaced the select parameters with the following:
<asp:ControlParameter Name="CustomerSearch" ControlID="cboCustomerID" PropertyName ="SelectedValue" />
<asp:ControlParameter Name="StaffSearch" ControlID="cboStaffID" PropertyName ="SelectedValue" />
<asp:ControlParameter Name="ShowClosed" Type="Boolean" ControlID="chkShowClosed" PropertyName="Checked" />
...and removed the extra code in the btnSearch_Click event, so the only line in that code is:
protected void btnSearch_Click(object sender, EventArgs e)
{
gvJobs.DataBind();
}
...no change. Still nothing happens when I click the search button.
Yay! Found the answer:
https://forums.asp.net/t/1243253.aspx?Gridview+Databind+Not+Working
The issue was that the gridviews have a property: CancelSelectOnNullParameter, which is true by default. Since at least one of the drop down lists is almost always null when I do the search, this resulted in nothing happening when I hit the search button. When I added CancelSelectOnNullParameter="false" to the gridview control, it fixed the problem.
(Semi-related note) I also added the following to the ControlParameters: ConvertEmptyStringToNull="true"

GridView Update button execute update SQL Server stored procedure

SQL Server stored procedure accepts the parameters, current company name, new company name and whether it already exists which has a default value. When the edit button is clicked on the front end UI, the grid view allows me to edit the company name. This shows an 'Update' button - when this is clicked - the code parses however nothing updates and company name does not update either.
Breakpoint is set and stepped through and #CurrentCompanyName is returned as null. Not sure how to fix this.
Aspx:
<asp:GridView ID="CompanyTable" runat="server"
OnRowEditing="CompanyTable_RowEditing"
OnRowCancelingEdit="CompanyTable_RowCancelingEdit"
OnRowUpdating="CompanyTable_RowUpdating"
OnPageIndexChanging="CompanyTable_PageIndexChanging"
PageSize="20" Font-Underline="False" AllowPaging="True">
<HeaderStyle Width="150px" />
<Columns>
<asp:TemplateField>
<HeaderStyle Width="200px" />
<ControlStyle CssClass="ButtonDesigntwo" />
<ItemTemplate>
<asp:LinkButton ID="Edit" ButtonType="Button" runat="server" CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="Delete" ButtonType="Button" runat="server" CommandName="Delete" Text="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="Update" ButtonType="Button" runat="server" Text="Update" CommandName="Update"/>
<asp:Button ID="Cancel" ButtonType="Button" runat="server" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="tableHeaderStyle" />
<PagerSettings Mode="NumericFirstLast" />
<PagerStyle CssClass="pager" BorderColor="Black" ForeColor="White" Font-Underline="False" />
<RowStyle CssClass="tableRowStyle" />
</asp:GridView>
Method code:
protected void CompanyTable_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
SqlConnection cn = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand("[updateCompanyName]", cn))
{
TextBox name = CompanyTable.Rows[e.RowIndex].FindControl("CompanyTable") as TextBox;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#CurrentCompanyName", name);
cmd.Parameters.AddWithValue("#NewCompanyName", CompanyInputTextBox.Text).Direction = ParameterDirection.Input;
SqlParameter objisExists = new SqlParameter("#isExists", SqlDbType.Int);
objisExists.Direction = ParameterDirection.Output;
cmd.Parameters.Add(objisExists);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
int isExists = Convert.ToInt32(cmd.Parameters["#isExists"].Value.ToString());
if (isExists == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "111", "AddCompanyUpdating();", true);
}
else if (isExists == 1)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "111", "CompanyNotUpdatedValidation();", true);
}
}
// Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
CompanyTable.EditIndex = -1;
// Call ShowData method for displaying updated data
BindData();
}
Stored procedure:
ALTER PROCEDURE [dbo].[updateCompanyName]
#CurrentCompanyName VARCHAR(50),
#NewCompanyName VARCHAR(50),
#IsExists INT = 0 OUT
AS
BEGIN
DECLARE #CompanyID INT
SELECT #CompanyID = CompanyID
FROM company
WHERE companyname = #CurrentCompanyName
BEGIN
IF EXISTS (SELECT CompanyName
FROM company
WHERE companyname = #NewCompanyName )
BEGIN
SET #IsExists = 1
END
ELSE
BEGIN
UPDATE COMPANY
SET CompanyName = #NewCompanyName
WHERE companyid = #CompanyID
SET #IsExists = 0
END
END
PRINT #isexists
END
You are defining name as a textbox in this line:
TextBox name = CompanyTable.Rows[e.RowIndex].FindControl("CompanyTable") as TextBox;
Then you try to set the value of your parameter to the textbox in this line:
cmd.Parameters.AddWithValue("#CurrentCompanyName", name);
When what you SHOULD be doing, is setting the parameter value to the TEXT that is IN the textbox:
cmd.Parameters.AddWithValue("#CurrentCompanyName", name.Text);
EDIT:
Since name itself is NULL when you are hovering over it, that means that you have not correctly defined it in this line:
TextBox name = CompanyTable.Rows[e.RowIndex].FindControl("CompanyTable") as TextBox;
Stop the code in the debugger and open a quick view into CompanyTable, and see if you can figure out the correct way to define the textbox you are looking for.
EDIT 2: In defining your TextBox, you are doing FindControl("CompanyTable"), but according to your markup, "CompanyTable" is the ID of your GridView, not a textbox. In fact, I don't see any markup anywhere for a textbox in the first code sample you posted.

Server Control as a parameter to SqlDataSource to filter results

Problem
I am trying to use a text box txtSearch to filter results from a SqlDataSource.
My query:
SELECT
[id], [username], [name]
FROM
[dbo].[users]
WHERE
#filter IS NULL OR
LEN(#filter) = 0 OR
[name] LIKE ('%'+#filter+'%')
ASPX markup:
<div>
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" CausesValidation="False" OnClick="btnSearch_Click" />
</div>
<asp:SqlDataSource ID="SQLDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT [id], [username], [name] FROM [dbo].[users] WHERE #filter IS NULL OR LEN(#filter) = 0 OR [name] LIKE ('%'+#filter+'%')">
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" DefaultValue=""
Name="filter" Type="String" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
C# code-behind:
protected void btnSearch_Click(object sender, EventArgs e)
{
GridView1.DataBind();
}
Table [dbo].[users]:
id name username
---- ---- --------
0 Jack jhenry
1 Jim jcallaway
2 Phillip pmcarthur
I simply get an empty grid, then I tried to set the default value to " " (single space) and modified #filter IS NULL to #filter = ''. This gave me the whole grid as expected but when I enter a value in the textbox and hit Search, nothing happens. How do I get the filter hooked up to the SqlDataSource with least effort?
Dirty fix:
Searching for a space " " instead of NULL works, but I have set ConvertEmptyStringToNull = True
SELECT
[id], [username], [name]
FROM
[dbo].[users]
WHERE
#filter = ' ' OR
LEN(#filter) = 0 OR
[name] LIKE ('%'+#filter+'%')
I would rather use IS NULL to make it clean, is it possible?
Could you add Type="String" to ControlParameter and try with the following query?
SELECT [id], [username], [name]
FROM [dbo].[users]
WHERE LTRIM(RTRIM(#filter)) = ''
OR [name] LIKE '%'+ LTRIM(RTRIM(#filter)) +'%'
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch"
DefaultValue=""
Name="filter"
Type="String"
PropertyName="Text" />
</SelectParameters>
Updated:
You need to rebind datasource instead of Grid.
protected void btnSearch_Click(object sender, EventArgs e)
{
SQLDataSource1.DataBind();
}
Stored procedure:
If you want validation logic, it is better to use a stored procedure.
For example:
CREATE PROCEDURE GetUsers
#Filter NVARCHAR(100) = NULL
AS
BEGIN
DECLARE #SearchFilter BIT
SET #SearchFilter = 1
IF (#Filter IS NULL OR RTRIM(LTRIM(#Filter)) = N'')
SET #SearchFilter = 0
SET #Filter = ISNULL(#Filter, '')
SET #Filter = '%' + RTRIM(LTRIM(#Filter)) + '%'
SELECT [id], [username], [name]
FROM [dbo].[users] WITH (NOLOCK)
WHERE #SearchFilter = 0
OR [name] LIKE #Filter
END

Custom SQL Select statement not displaying data in GridView

SELECT
LeagueTable.P,
LeagueTable.W,
LeagueTable.D,
LeagueTable.L,
LeagueTable.GF,
LeagueTable.GA,
LeagueTable.GD,
LeagueTable.Pts,
Team.Team_name,
LeagueTable.Team_ID
FROM LeagueTable
INNER JOIN Team
ON LeagueTable.Team_ID = Team.Team_ID
I've got the user to enter a team name on start up that is entered into the Team table, which redirects to a webpage with a league table. The league table initially contains no data but it should be displaying a row with the users input once the user has gotten to this page.
However this query shows the GridView as blank. What's wrong with it?
asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="No teams entered into the table.">
<Columns>
<asp:BoundField DataField="P" HeaderText="P" SortExpression="P" />
<asp:BoundField DataField="W" HeaderText="W" SortExpression="W" />
<asp:BoundField DataField="D" HeaderText="D" SortExpression="D" />
<asp:BoundField DataField="L" HeaderText="L" SortExpression="L" />
<asp:BoundField DataField="GF" HeaderText="GF" SortExpression="GF" />
<asp:BoundField DataField="GA" HeaderText="GA" SortExpression="GA" />
<asp:BoundField DataField="GD" HeaderText="GD" SortExpression="GD" />
<asp:BoundField DataField="Pts" HeaderText="Pts" SortExpression="Pts" />
<asp:BoundField DataField="Team_name" HeaderText="Team_name" SortExpression="Team_name" />
<asp:BoundField DataField="Team_ID" HeaderText="Team_ID" SortExpression="Team_ID" />
</Columns>
</asp:GridView>
protected void NewTeamBtn_Click(object sender, EventArgs e)
{
string qry1 = "INSERT into Team (Team_name) VALUES (#Team_name)";
using (SqlCommand cmd = new SqlCommand(qry1, con))
{
cmd.Parameters.Add(("#Team_name"), SqlDbType.VarChar).Value = NewTeamTxtBox.Text;
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Response.Redirect("EnterData.aspx");
}
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SportsData2ConnectionString %>" SelectCommand="SELECT LeagueTable.League_ID, LeagueTable.Team_ID, LeagueTable.P, LeagueTable.W, LeagueTable.D, LeagueTable.L, LeagueTable.GF, LeagueTable.GA, LeagueTable.GD, LeagueTable.Pts, Team.Team_name FROM LeagueTable INNER JOIN Team ON LeagueTable.Team_ID = Team.Team_ID"></asp:SqlDataSource>
Based on your comments it appears you may be inserting data into your Team table, but when you run the select statement you have in your SQL Data Source you will notice you have an Inner Join. That Inner Join means that if the team Id is not in either one of the 2 tables you are joining then no results will be returned.
I don't see your sql data source code, but another possible point of failure is the TeamID you pass into that control. There are 3 questions that come to my mind for making sure you get valid data.
1) Where are you getting it from?
2) Is it valid and in both tables?
3) Are you rebinding the gridview after setting that value?
Edit
What you want to do does not at all match your question at this point. You should edit your title and question.
I would use something like this for your insert.
DECLARE #TeamID as INT
INSERT into Team (Team_name) VALUES (#Team_name)
SELECT #TeamID = SCOPE_IDENTITY();
INSERT into LeagueTable Team_ID VALUES #TeamID
this post will help Scope_Identity vs ##Identity
this stack question will help too.

how to filter a gridview

I have a gridview connected to a database by sqldatasource. I have multiple drop down lists and a table where users can select different data to filter the gridview.
I know how to set this up for one control but have no idea how to do it for multiple ones. Ideally what I would like to do is onfilterButton_Click the code behind connects to a stored procedure in the database. The parameters would depend on the filter options the user has chosen. I just don't know how to write the stored procedure. (I am not the best at SQL)
But I am open to suggestions of better ways to do this. I am using c# visual studios 2010 and sql server 2008.
I have been stuck on this for over a week now so really any practical help would be welcome.
Write One Stored Procedure and Pass DropDownList values as Parameters like this:
CREATE PROC CUST_Details
(
#CustomerID INT,
#CompanyID INT
)
AS
BEGIN
SELECT
Customer.CustomerName,
Company.CompanyName
FROM
Customer INNER JOIN
Company ON Customer.CompanyID = Company.CompanyID
WHERE
(#CompanyID = -1 OR Customer.CompanyID = #CompanyID) AND
(#CustomerID = -1 OR Customer.CustomerID = #CustomerID)
END
Before that, in your DropDownList add item such as "<-----Select----->" as DataTextField and "-1" as DataValueField.
In Both DropDownList SelectedIndexChanged Event, pass DropDownList's SelectedValue as parameters and once again call the database and databind it with the Grid.
If you are not good in writing SP's then you can directly filter your SQL DataSource by using the FilterExpression property.
sql.FilterExpression = "Filteration Expression";
GridView1.DataBind();
Edit: Something that will work for you:
if (DropDownList1.SelectedItem.Text != "All")
{
SqlDataSource1.FilterExpression = "Title like '" + textbox1.Text + "' and Category like " + DropDownList1.SelectedValue;
}
else
{
SqlDataSource1.FilterExpression = "Title like '" + textbox1.Text + "'";
}
GridView1.DataBind();
Below might give you idea how this works, please check:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="FORM1" runat="server">
<p>Show all employees with the following title:
<asp:DropDownList
id="DropDownList1"
runat="server"
AutoPostBack="True">
<asp:ListItem>Sales Representative</asp:ListItem>
<asp:ListItem>Sales Manager</asp:ListItem>
<asp:ListItem>Vice President, Sales</asp:ListItem>
</asp:DropDownList></p>
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
FilterExpression="Title='{0}'" OnFiltering="SqlDataSource1_Filtering">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</FilterParameters>
</asp:SqlDataSource><br />
<asp:GridView
id="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<columns>
<asp:BoundField Visible="False" DataField="EmployeeID" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</columns>
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>
Server Side:
protected void SqlDataSource1_Filtering(object sender, SqlDataSourceFilteringEventArgs e)
{
Label1.Text = e.ParameterValues[0].ToString();
}
Your stored procedure would declare all your parameters like so:
CREATE PROCEDURE GetData
#parameter1 varchar(50),
#parameter2 int
etc...
Then you would call your stored procedure with the parameters like:
GetData(dropdownlist1.SelectedItem.Value, dropdownlist2.SelectedItem.Value);
Is that the sort of thing you mean?
UPDATE:
For a situation where the 'All' option was chosen, you could still pass the value as a null parameter, and in the stored procedure you would declare it as 'optional':
CREATE PROCEDURE GetData
#parameter1 varchar(50) = NULL,
#parameter2 int
So the query in your stored procedure would look something like this:
SELECT *
FROM Table
WHERE ((#parameter1 IS NULL) OR (column1 = #parameter1 ))
AND column2 = #parameter2

Categories