I have a database that stores lottery form submissions from users. One of the columns is a DateTime column that is populated with the current DateTime every time someone submits a form. I want to be able to search by the DateTime column so I can pull up all the forms entered on a particular day.
I'm not getting any errors when I perform the search, just my custom message that "No results matching that search criteria". I've tried disabling the jquery datepicker and tried copying and pasting the exact DateTime field from the DB into the search box and still nothing. Obviously I'm missing something but I can't figure out what. Any help is much appreciated.
C# code:
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.Configuration;
public partial class adminDefault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void lastNameButton(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT First, Last, Addr1, C, S, Z, dayphone, eveningphone, email, tripCC, tripCCexpiration, tripCVV, tripCCname, memberCC, memberCCexpiration, memberCVV, memberCCname, membership, hutCredit, creditName, GroupName, Id, LotteryChoices, dateTime FROM Lotto WHERE Last LIKE '%'+ #Name + '%'", conn);
comm.Parameters.Add("#Name", System.Data.SqlDbType.NVarChar, 50).Value = nameSearch.Text;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (!reader.HasRows)
{
noMatchLabel.Text = "No results matching that search criteria.";
}
else
{
grid.DataSource = reader;
grid.DataKeyNames = new string[] { "Id" };
grid.DataBind();
reader.Close();
conn.Close();
noMatchLabel.Text = null;
}
}
protected void groupNameButton(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT First, Last, Addr1, C, S, Z, dayphone, eveningphone, email, tripCC, tripCCexpiration, tripCVV, tripCCname, memberCC, memberCCexpiration, memberCVV, memberCCname, membership, hutCredit, creditName, GroupName, Id, LotteryChoices, dateTime FROM Lotto WHERE GroupName LIKE '%'+ #GroupName + '%'", conn);
comm.Parameters.Add("#GroupName", System.Data.SqlDbType.NVarChar, 50).Value = groupSearch.Text;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (!reader.HasRows)
{
noMatchLabel.Text = "No results matching that search criteria.";
}
else
{
grid.DataSource = reader;
grid.DataKeyNames = new string[] { "Id" };
grid.DataBind();
reader.Close();
conn.Close();
noMatchLabel.Text = null;
}
}
protected void dateSearch(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT First, Last, Addr1, C, S, Z, dayphone, eveningphone, email, tripCC, tripCCexpiration, tripCVV, tripCCname, memberCC, memberCCexpiration, memberCVV, memberCCname, membership, hutCredit, creditName, GroupName, Id, LotteryChoices, dateTime FROM Lotto WHERE dateTime = #dateTime ", conn);
comm.Parameters.Add("#dateTime", System.Data.SqlDbType.DateTime).Value = DateTime.Parse(datesearch.Text);
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (!reader.HasRows)
{
noMatchLabel.Text = "No results matching that search criteria.";
}
else
{
grid.DataSource = reader;
grid.DataKeyNames = new string[] { "Id" };
grid.DataBind();
reader.Close();
conn.Close();
noMatchLabel.Text = null;
}
}
protected void grid_SelectedIndexChanged(object sender, EventArgs e)
{
BindLottoDetails();
}
private void BindLottoDetails()
{
int selectedRowIndex = grid.SelectedIndex;
int lottoId = (int)grid.DataKeys[selectedRowIndex].Value;
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT Id, First, Last, Addr1, C, S, Z, dayphone, eveningphone, email, tripCC, tripCCexpiration, tripCVV, tripCCname, memberCC, memberCCexpiration, memberCVV, memberCCname, membership, hutCredit, creditName, GroupName, LotteryChoices, dateTime FROM Lotto WHERE Id=#Id", conn);
comm.Parameters.Add("Id", System.Data.SqlDbType.Int);
comm.Parameters["Id"].Value = lottoId;
try
{
conn.Open();
reader = comm.ExecuteReader();
lottoFormDetails.DataSource = null;
lottoFormDetails.DataSource = reader;
lottoFormDetails.DataKeyNames = new string[] { "Id" };
lottoFormDetails.DataBind();
reader.Close();
}
finally
{
conn.Close();
}
}
protected void lottoFormDetails_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
lottoFormDetails.ChangeMode(e.NewMode);
BindLottoDetails();
}
protected void lottoFormDetails_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
int LottoId = (int)lottoFormDetails.DataKey.Value;
TextBox newFirstTextBox = (TextBox)lottoFormDetails.FindControl("editFirstTextBox");
TextBox newLastTextBox = (TextBox)lottoFormDetails.FindControl("editLastTextBox");
TextBox newAddr1TextBox = (TextBox)lottoFormDetails.FindControl("editAddr1TextBox");
TextBox newCityTextBox = (TextBox)lottoFormDetails.FindControl("editCityTextBox");
TextBox newStateTextBox = (TextBox)lottoFormDetails.FindControl("editStateTextBox");
TextBox newZipTextBox = (TextBox)lottoFormDetails.FindControl("editZipTextBox");
TextBox newdaytimephoneTextBox = (TextBox)lottoFormDetails.FindControl("editdaytimephoneTextBox");
TextBox neweveningphoneTextBox = (TextBox)lottoFormDetails.FindControl("editeveningphoneTextBox");
TextBox newemailTextBox = (TextBox)lottoFormDetails.FindControl("editemailTextBox");
TextBox newmembershipTextBox = (TextBox)lottoFormDetails.FindControl("editmembershipTextBox");
TextBox newhutCreditTextBox = (TextBox)lottoFormDetails.FindControl("edithutCreditTextBox");
TextBox newcreditNameTextBox = (TextBox)lottoFormDetails.FindControl("editcreditNameTextBox");
TextBox newGroupNameBox = (TextBox)lottoFormDetails.FindControl("editGroupNameTextBox");
TextBox newLotteryChoicesTextBox = (TextBox)lottoFormDetails.FindControl("editLotteryChoicesTextBox");
string newFirst = newFirstTextBox.Text;
string newLast = newLastTextBox.Text;
string newAddr1 = newAddr1TextBox.Text;
string newCity = newCityTextBox.Text;
string newState = newStateTextBox.Text;
string newZip = newZipTextBox.Text;
string newdaytimephone = newdaytimephoneTextBox.Text;
string neweveningphone = neweveningphoneTextBox.Text;
string newemail = newemailTextBox.Text;
string newmembership = newmembershipTextBox.Text;
string newhutCredit = newhutCreditTextBox.Text;
string newcreditName = newcreditNameTextBox.Text;
string newGroupName = newGroupNameBox.Text;
string newLotteryChoices = newLotteryChoicesTextBox.Text;
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["10thLottoApp"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("UpdateLotteryForm", conn);
comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.Parameters.Add("Id", System.Data.SqlDbType.Int);
comm.Parameters["Id"].Value = LottoId;
comm.Parameters.Add("NewFirst", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewFirst"].Value = newFirst;
comm.Parameters.Add("NewLast", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewLast"].Value = newLast;
comm.Parameters.Add("NewAddr1", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewAddr1"].Value = newAddr1;
comm.Parameters.Add("NewC", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewC"].Value = newCity;
comm.Parameters.Add("NewS", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewS"].Value = newState;
comm.Parameters.Add("NewZ", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewZ"].Value = newZip;
comm.Parameters.Add("Newdayphone", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["Newdayphone"].Value = newdaytimephone;
comm.Parameters.Add("Neweveningphone", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["Neweveningphone"].Value = neweveningphone;
comm.Parameters.Add("Newemail", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["Newemail"].Value = newemail;
comm.Parameters.Add("Newmembership", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["Newmembership"].Value = newmembership;
comm.Parameters.Add("NewhutCredit", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewhutCredit"].Value = newhutCredit;
comm.Parameters.Add("NewcreditName", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewcreditName"].Value = newcreditName;
comm.Parameters.Add("NewGroupName", System.Data.SqlDbType.NVarChar, 50);
comm.Parameters["NewGroupName"].Value = newGroupName;
comm.Parameters.Add("NewLotteryChoices", System.Data.SqlDbType.NVarChar, -1);
comm.Parameters["NewLotteryChoices"].Value = newLotteryChoices;
try
{
conn.Open();
comm.ExecuteNonQuery();
}
finally
{
conn.Close();
}
lottoFormDetails.ChangeMode(DetailsViewMode.ReadOnly);
BindLottoDetails();
}
}
.aspx code
<%# Page Title="" Language="C#" MasterPageFile="~/admin.master" AutoEventWireup="true" CodeFile="adminDefault.aspx.cs" Theme="Blue" Inherits="adminDefault" %>
<%# Import Namespace = "System.Data.SqlClient" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js" ></script>
<script>
$(function () {
$("#<%=datesearch.ClientID%>").datepicker();
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="container">
<div class="row">
<div class="col-md-12">
<h2>10th Mountain Lottery App</h2>
</div>
</div>
<div class="row">
<div class="col-md-2 sidenav">
<ul>
<li>Search Forms</li>
<li>Display Full Lottery List</li>
<li>Error Filtering</li>
</ul>
</div>
<div class="col-md-10">
<p>You can use the menu below to search for individual forms</p>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h4>Lottery Form Search</h4>
<asp:Label runat="server">Search by Last Name: <asp:TextBox id="nameSearch" runat="server"></asp:TextBox></asp:Label>
<p><asp:Button id="nameSearchButton" runat="server" Text="Search" OnClick="lastNameButton" /></p>
<asp:Label runat="server">Search by Group Name: <asp:TextBox id="groupSearch" runat="server"></asp:TextBox></asp:Label>
<p><asp:Button id="groupsearchButton" runat="server" Text="Search" OnClick="groupNameButton" /></p>
<asp:Label runat="server">Search by Date: <asp:TextBox id="datesearch" runat="server"></asp:TextBox></asp:Label>
<p><asp:Button id="dateSearchButton" runat="server" Text="Search" OnClick="dateSearch" /></p>
<asp:Label ID="noMatchLabel" runat="server"></asp:Label>
<asp:GridView id="grid" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="grid_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="First" HeaderText="First Name" />
<asp:BoundField DataField="Last" HeaderText="Last Name" />
<asp:BoundField DataField="C" HeaderText="City" />
<asp:BoundField DataField="GroupName" HeaderText="Group Name" />
<asp:ButtonField CommandName="Select" Text="Select" />
</Columns>
</asp:GridView>
<br />
<asp:DetailsView ID="lottoFormDetails" runat="server" AutoGenerateRows="False" OnItemUpdating="lottoFormDetails_ItemUpdating" OnModeChanging="lottoFormDetails_ModeChanging">
<Fields>
<asp:TemplateField HeaderText="First Name">
<EditItemTemplate>
<asp:TextBox ID="editFirstTextBox" runat="server" Text='<%# Bind("First") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertFirstTextBox" runat="server" Text='<%# Bind("First") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="FirstLabel" runat="server" Text='<%# Bind("First") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<EditItemTemplate>
<asp:TextBox ID="editLastTextBox" runat="server" Text='<%# Bind("Last") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertLastTextBox" runat="server" Text='<%# Bind("Last") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="LastLabel" runat="server" Text='<%# Bind("Last") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<EditItemTemplate>
<asp:TextBox ID="editAddr1TextBox" runat="server" Text='<%# Bind("Addr1") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertAddr1TextBox" runat="server" Text='<%# Bind("Addr1") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Addr1Label" runat="server" Text='<%# Bind("Addr1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<EditItemTemplate>
<asp:TextBox ID="editCityTextBox" runat="server" Text='<%# Bind("C") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertCityTextBox" runat="server" Text='<%# Bind("C") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="CityLabel" runat="server" Text='<%# Bind("C") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<EditItemTemplate>
<asp:TextBox ID="editStateTextBox" runat="server" Text='<%# Bind("S") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertStateTextBox" runat="server" Text='<%# Bind("S") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="StateLabel" runat="server" Text='<%# Bind("S") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Zip">
<EditItemTemplate>
<asp:TextBox ID="editZipTextBox" runat="server" Text='<%# Bind("Z") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertZipTextBox" runat="server" Text='<%# Bind("Z") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="ZipLabel" runat="server" Text='<%# Bind("Z") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Daytime Phone #">
<EditItemTemplate>
<asp:TextBox ID="editdaytimephoneTextBox" runat="server" Text='<%# Bind("dayphone") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertdaytimephoneTextBox" runat="server" Text='<%# Bind("dayphone") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="daytimephoneLabel" runat="server" Text='<%# Bind("dayphone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Evening Phone #">
<EditItemTemplate>
<asp:TextBox ID="editeveningphoneTextBox" runat="server" Text='<%# Bind("eveningphone") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="inserteveningphoneTextBox" runat="server" Text='<%# Bind("eveningphone") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="eveningphoneLabel" runat="server" Text='<%# Bind("eveningphone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<EditItemTemplate>
<asp:TextBox ID="editemailTextBox" runat="server" Text='<%# Bind("email") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertemailTextBox" runat="server" Text='<%# Bind("email") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="emailLabel" runat="server" Text='<%# Bind("email") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Membership">
<EditItemTemplate>
<asp:TextBox ID="editmembershipTextBox" runat="server" Text='<%# Bind("membership") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertmembershipTextBox" runat="server" Text='<%# Bind("membership") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="membershipLabel" runat="server" Text='<%# Bind("membership") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hut Credit">
<EditItemTemplate>
<asp:TextBox ID="edithutCreditTextBox" runat="server" Text='<%# Bind("hutCredit") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="inserthutCreditTextBox" runat="server" Text='<%# Bind("hutCredit") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="hutCreditLabel" runat="server" Text='<%# Bind("hutCredit") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hut Credit Name">
<EditItemTemplate>
<asp:TextBox ID="editcreditNameTextBox" runat="server" Text='<%# Bind("creditName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertcreditNameTextBox" runat="server" Text='<%# Bind("creditName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="creditNameLabel" runat="server" Text='<%# Bind("creditName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group Name">
<EditItemTemplate>
<asp:TextBox ID="editGroupNameTextBox" runat="server" Text='<%# Bind("GroupName") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertGroupNameTextBox" runat="server" Text='<%# Bind("GroupName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%# Bind("GroupName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lottery Choices">
<EditItemTemplate>
<asp:TextBox ID="editLotteryChoicesTextBox" TextMode="multiline" Columns="75" Rows="10" runat="server" Text='<%# Bind("LotteryChoices") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="insertLotteryChoicesTextBox" TextMode="multiline" Columns="75" Rows="10" runat="server" Text='<%# Bind("LotteryChoices") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="LotteryChoicesLabel" TextMode="multiline" Columns="75" Rows="10" runat="server" Text='<%# Bind("LotteryChoices") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
<HeaderTemplate>
<%#Eval("First")%> <%#Eval("Last") %> <%#Eval("dateTime") %>
</HeaderTemplate>
</asp:DetailsView>
</div>
</div>
</div>
</div>
</div>
</asp:Content
>
In SQL, you state that you are storing the data as a DateTime. This means you are getting the date and the time. Of course the time includes hours, minutes, seconds, etc...
In you query you need to CAST your DateTime to a Date in order to remove the time from it.
See this MSDN article for more clarification https://msdn.microsoft.com/en-us/library/ms187819.aspx
Related
I have an editable GridView but I would also like to have an add new row functionality, so I made a button using FooterTemplate for all editable fields and set CommandName="AddNew" . Everything looks as expected on the front end, but Add New Row cannot find txtGrantId, txtPotId or txtBudget at all and throws System.NullReferenceException 'Object reference not set to an instance of an object.'. The issue has to be in the code behind, but I am attaching the front end as well. The data source is pretty long but it does call OnRowCommand="gvPotsMoneyGrants_RowCommand"
ASPX
<%--Primary Key LinkId--%>
<asp:TemplateField HeaderText="LinkId" SortExpression="LinkId">
<ItemTemplate>
<asp:Label ID="lblLinkId" runat="server" Text='<%# Eval("LinkId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%--GrantId--%>
<asp:TemplateField HeaderText="GrantId" SortExpression="GrantId">
<ItemTemplate>
<asp:Label ID="lblGrantIdMain" runat="server" Text='<%#Eval("GrantId") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGrantId" runat="server" Text='<%#Bind("GrantId") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inGrantId" Width="120px" runat="server" />
<asp:RequiredFieldValidator ID="vGrantId" runat="server" ControlToValidate="inGrantId" Text="?" ValidationGroup="VG5" />
</FooterTemplate>
</asp:TemplateField>
<%--PotId--%>
<asp:TemplateField HeaderText="PotId" SortExpression="PotId">
<ItemTemplate>
<asp:Label ID="lblPotIdMain" runat="server" Text='<%#Eval("PotId") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPotId" runat="server" Text='<%#Bind("PotId") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inPotId" Width="120px" runat="server" />
<asp:RequiredFieldValidator ID="vPotId" runat="server" ControlToValidate="inPotId" Text="?" ValidationGroup="VG5" />
</FooterTemplate>
</asp:TemplateField>
<%--Budget--%>
<asp:TemplateField HeaderText="Budget" SortExpression="Budget">
<ItemTemplate>
<asp:Label ID="lblBudgetMain" runat="server" Text='<%#Eval("Budget") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBudget" runat="server" Text='<%#Bind("Budget") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="inBudget" Width="120px" runat="server" />
<asp:RequiredFieldValidator ID="vBudget" runat="server" ControlToValidate="inBudget" Text="?" ValidationGroup="VG5" />
</FooterTemplate>
</asp:TemplateField>
Code behind
protected void gvPotsMoneyGrants_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtGrantId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtGrantId");
TextBox txtPotId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtPotId");
TextBox txtBudget = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("txtBudget");
string GrantId, PotId, Budget;
GrantId = txtGrantId.Text;
PotId = txtPotId.Text;
Budget = txtBudget.Text;
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("GrantId", GrantId);
cmd.Parameters.AddWithValue("PotId", PotId);
cmd.Parameters.AddWithValue("Budget", Budget);
}
}
Please, try the code below.
protected void gvPotsMoneyGrants_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox inGrantId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inGrantId");
TextBox inPotId = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inPotId");
TextBox inBudget = (TextBox)gvPotsMoneyGrants.FooterRow.FindControl("inBudget");
string GrantId, PotId, Budget;
GrantId = inGrantId.Text;
PotId = inPotId.Text;
Budget = inBudget.Text;
SqlCommand cmd = new SqlCommand();
cmd.Parameters.AddWithValue("GrantId", GrantId);
cmd.Parameters.AddWithValue("PotId", PotId);
cmd.Parameters.AddWithValue("Budget", Budget);
}
}
I am binding the data into radio buttons, but error that: DataBinding: 'System.Char' does not contain a property with the name 'ProfileRbnName'..
and my Db Table is
Profile_RbnFilter
Columns are
ID, ProfileRbnName
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetGridData();
RadioButton_Bind();
}
}
public void GetGridData()
{
DataTable tableObject = new DataTable();
string sqlstr = "select a.SID as ProfileID,a.StaffName as Name,b.Designation,a.Staffqualification as Qualification,a.Uploadphoto as ImageUrl,case a.Status when '1' then 'Active' when '2' then 'InActive' end as Status,convert(varchar(12),a.date,101) as Date from Adding_New_Staff a join Dental_Designation b on a.StaffDesignation=b.Did order by a.sid";
tableObject = DAL.getData(sqlstr);
GridProfile.DataSource = tableObject;
GridProfile.DataBind();
}
private void RadioButton_Bind()
{
string cmdstr = "select ID,ProfileRbnName from Profile_RbnFilter";
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmdstr, con);
adp.Fill(ds);
RbnFilter.DataSource = cmdstr;
RbnFilter.DataValueField = "ID";
RbnFilter.DataTextField = "ProfileRbnName";
RbnFilter.DataBind();
}
[<span style="font-size: 14px; font-weight: normal;">
<asp:RadioButtonList ID="RbnFilter" runat="server"
RepeatDirection="Horizontal" OnSelectedIndexChanged="rbnSearchby_SelectedIndexChanged"
RepeatLayout="Flow" AutoPostBack="True">
<asp:ListItem Text="All" Value="10" Selected="true" />
<asp:ListItem Text="Doctors" Value="11" />
<asp:ListItem Text="Dental Hygienist & Assistants" Value="12" />
<asp:ListItem Text="Nurses" Value="13" />
<asp:ListItem Text="Front Office" Value="14" />
</asp:RadioButtonList>
</span>]
</h1>
</div>
<div>
<asp:GridView ID="GridProfile" runat="server" AutoGenerateColumns="False"
Width="100%" ShowHeaderWhenEmpty="True" class="Grid">
<Columns>
<asp:TemplateField HeaderText="Profile ID">
<ItemTemplate>
<asp:Label ID="lblProfileID" runat="server" Text='<%# Eval("ProfileID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<ItemTemplate>
<asp:Label ID="lblDesignation" runat="server" Text='<%# Eval("Designation") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qualification">
<ItemTemplate>
<asp:Label ID="lblQualification" runat="server" Text='<%# Eval("Qualification") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image Url">
<ItemTemplate>
<asp:Label ID="lblImageUrl" runat="server" Text='<%# Eval("ImageUrl") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lbldate" runat="server" Text='<%# Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
private void RadioButton_Bind()
{ cmdstr = "select ID,ProfileRbnName from Profile_RbnFilter";
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter(cmdstr, con);
adp.Fill(ds);
RbnFilter.DataSource = ds.Tables[0];
RbnFilter.DataValueField = "ID";
RbnFilter.DataTextField = "ProfileRbnName";
RbnFilter.DataBind();
};
I've coded a gridview that doesn't fire when I click the edit button and I found an older question that addresses my issue by just using e.CommandName with if statements under the RowCommand method. I'm trying to figure out how to implement it with my code.
My question is, how do I use e.RowIndex during update to find my controls and reference those now? Also, I've tried calling my old Update method but it won't let me use sender and e as parameters because GridviewCommandEventArgs is different from GridViewEventUpdateArgs. I'm pretty lost, any help would be appreciated in straightening this out.
C#:
void RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Edit")
{
}
if (e.CommandName == "Update")
{
UpdateCustomer(sender, e);
string nFirstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;
string nLastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;
string nEmergency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmergency")).Text;
string nCell = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCell")).Text;
string nAge = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAge")).Text;
string nActivityCard = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtActivityCard")).Text;
string nBoat = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBoat")).Text;
string nInitials = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtInitials")).Text;
string nGroup = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGroup")).Text;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update Person set FirstName=#FirstName, LastName=#LastName, " +
"Emergency#=#Emergency, Cell#=#Cell, Age=#Age, ActivityCard=#ActivityCard, Initials=#Initials, CraftType=#Boat, Group#=#Group " +
"where Person.PersonID=#Pid;" +
"SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
"Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
"FROM Person INNER JOIN " +
"TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = #Date and " +
"TripSchedule.Time = #Time and TripSchedule.TripType = #Type order by Person.Group#;";
cmd.Parameters.Add("#FirstName", SqlDbType.VarChar).Value = nFirstName;
cmd.Parameters.Add("#LastName", SqlDbType.VarChar).Value = nLastName;
cmd.Parameters.Add("#Emergency", SqlDbType.NChar).Value = nEmergency;
cmd.Parameters.Add("#Cell", SqlDbType.NChar).Value = nCell;
cmd.Parameters.Add("#Age", SqlDbType.NChar).Value = nAge;
cmd.Parameters.Add("#ActivityCard", SqlDbType.NChar).Value = nActivityCard;
cmd.Parameters.Add("#Initials", SqlDbType.NChar).Value = nInitials;
cmd.Parameters.Add("#Boat", SqlDbType.VarChar).Value = nBoat;
cmd.Parameters.Add("#Group", SqlDbType.VarChar).Value = nGroup;
cmd.Parameters.AddWithValue("#Date", TextBox1.Text);
cmd.Parameters.AddWithValue("#Time", ddlTripTime.SelectedItem.ToString());
cmd.Parameters.AddWithValue("#Type", ddlTripType.SelectedItem.ToString());
long personID = long.Parse(GridView1.DataKeys[e.RowIndex].Values["PersonID"].ToString());
cmd.Parameters.AddWithValue("#Pid", personID);
GridView1.EditIndex = -1;
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
if (e.CommandName == "Cancel")
{
}
}
ASP.NET:
<div id="dvGrid" style="padding: 0px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" DataKeyNames="PersonID" runat="server" AutoGenerateColumns="False"
Font-Names="Arial" Font-Size="10pt" AlternatingRowStyle-BackColor="blue" HeaderStyle-BackColor="aqua"
ShowFooter="true" OnRowEditing="EditCustomer" OnRowUpdating="UpdateCustomer"
OnRowCancelingEdit="CancelEdit" ShowHeaderWhenEmpty="true" Height="95px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Height="20px" Text='<%# Eval("FirstName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName")%>'></asp:TextBox>
</EditItemTemplate>
<ItemStyle Width = "60px" />
<FooterTemplate>
<asp:TextBox ID="txtFirstName" width="60px" MaxLength="15" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%#Bind("LastName")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastName" width="60px" MaxLength="15" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%# Eval("Age")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAge" Width="30px" MaxLength="2" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity Card">
<ItemTemplate>
<asp:Label ID="lblActivityCard" runat="server" Text='<%# Eval("ActivityCard")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtActivityCard" runat="server" Text='<%# Eval("ActivityCard")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtActivityCard" Width="50px" MaxLength="7" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cell Phone">
<ItemTemplate>
<asp:Label ID="lblCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCell" Width="70px" MaxLength="10" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emergency Phone">
<ItemTemplate>
<asp:Label ID="lblEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmergency" width="70px" MaxLength="10" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Boat Type">
<ItemTemplate>
<asp:Label ID="lblBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBoat" Width="80px" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Initials">
<ItemTemplate>
<asp:Label ID="lblInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtInitials" width="30px" MaxLength="2" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group #">
<ItemTemplate>
<asp:Label ID="lblGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtGroup" MaxLength="2" Width="20px" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("PersonID")%>'
OnClientClick="return confirm('Are you sure you want to delete?')" Text="Delete"
OnClick="DeleteCustomer"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Submit" OnClick="AddNewCustomer" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can pass RowIndex as CommandArgument for your LinkButtons.
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update" CommandArgument='<%# Container.DataItemIndex %>'>Update</asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel" CommandArgument='<%# Container.DataItemIndex %>'>Cancel</asp:LinkButton>
</EditItemTemplate>
Which can be access from code behind. Using rowindex you can get hold of the current row and find any controls using FindControl
void RowCommand(Object sender, GridViewCommandEventArgs e)
{
int rowindex = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Update")
{
....
}
if (e.CommandName == "Cancel")
{
....
}
}
e.CommandArgument will contain the index of the row clicked, so you can access the relevant controls via
int index = Convert.ToInt32(e.CommandArgument);
GridView1.Rows[index].Controls[x];
I have a gridview that has insert, update, and delete functionality. On my update statement, I'm not sure how to set my identifier PersonID = to the currently selected identifier in the gridview that is being updated. Is there a common method that people use to achieve this functionality?
Code for Asp.net GRIDVIEW
<div id="dvGrid" style="padding: 0px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
Font-Names="Arial" Font-Size="10pt" AlternatingRowStyle-BackColor="blue" HeaderStyle-BackColor="aqua"
ShowFooter="true" OnRowEditing="EditCustomer" OnRowUpdating="UpdateCustomer"
OnRowCancelingEdit="CancelEdit" Height="95px">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Height="20px" Text='<%# Eval("FirstName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFirstName" width="60px" MaxLength="15" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLastName" width="60px" MaxLength="15" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Text='<%# Eval("Age")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAge" Width="30px" MaxLength="2" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity Card">
<ItemTemplate>
<asp:Label ID="lblActivityCard" runat="server" Text='<%# Eval("ActivityCard")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtActivityCard" runat="server" Text='<%# Eval("ActivityCard")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtActivityCard" Width="50px" MaxLength="7" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cell Phone">
<ItemTemplate>
<asp:Label ID="lblCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCell" runat="server" Text='<%# Eval("Cell#")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCell" Width="70px" MaxLength="10" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emergency Phone">
<ItemTemplate>
<asp:Label ID="lblEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEmergency" runat="server" Text='<%# Eval("Emergency#")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEmergency" width="70px" MaxLength="10" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Boat Type">
<ItemTemplate>
<asp:Label ID="lblBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBoat" runat="server" Text='<%# Eval("CraftType")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBoat" Width="80px" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Initials">
<ItemTemplate>
<asp:Label ID="lblInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtInitials" runat="server" Text='<%# Eval("Initials")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtInitials" width="30px" MaxLength="2" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Group #">
<ItemTemplate>
<asp:Label ID="lblGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGroup" runat="server" Text='<%# Eval("Group#")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtGroup" MaxLength="2" Width="20px" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("PersonID")%>'
OnClientClick="return confirm('Are you sure you want to delete?')" Text="Delete"
OnClick="DeleteCustomer"></asp:LinkButton>
</itemtemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Submit" OnClick="AddNewCustomer" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<AlternatingRowStyle BackColor="#C2D69B" />
<HeaderStyle BackColor="Aqua" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
</div>
Method for Update:
protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
{
string nFirstName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFirstName")).Text;
string nLastName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLastName")).Text;
string nEmergency = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEmergency")).Text;
string nCell = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCell")).Text;
string nAge = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAge")).Text;
string nActivityCard = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtActivityCard")).Text;
string nBoat = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBoat")).Text;
string nInitials = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtInitials")).Text;
string nGroup = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGroup")).Text;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update Person set FirstName=#FirstName, LastName=#LastName, " +
"Emergency#=#Emergency, Cell#=#Cell, Age=#Age, ActivityCard=#ActivityCard, Initials=#Initials, CraftType=#Boat, Group#=#Group " +
"where Person.PersonID=Person.PersonID;" +
"SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
"Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
"FROM Person INNER JOIN " +
"TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = #Date and " +
"TripSchedule.Time = #Time and TripSchedule.TripType = #Type order by Person.Group#;";
cmd.Parameters.Add("#FirstName", SqlDbType.VarChar).Value = nFirstName;
cmd.Parameters.Add("#LastName", SqlDbType.VarChar).Value = nLastName;
cmd.Parameters.Add("#Emergency", SqlDbType.NChar).Value = nEmergency;
cmd.Parameters.Add("#Cell", SqlDbType.NChar).Value = nCell;
cmd.Parameters.Add("#Age", SqlDbType.NChar).Value = nAge;
cmd.Parameters.Add("#ActivityCard", SqlDbType.NChar).Value = nActivityCard;
cmd.Parameters.Add("#Initials", SqlDbType.NChar).Value = nInitials;
cmd.Parameters.Add("#Boat", SqlDbType.VarChar).Value = nBoat;
cmd.Parameters.Add("#Group", SqlDbType.VarChar).Value = nGroup;
cmd.Parameters.AddWithValue("#Date", TextBox1.Text);
cmd.Parameters.AddWithValue("#Time", ddlTripTime.SelectedItem.ToString());
cmd.Parameters.AddWithValue("#Type", ddlTripType.SelectedItem.ToString());
GridView1.EditIndex = -1;
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
First specify DataKeyNames="PersonID" for your gridview definition.
Here is how you get the PersonID value in your UpdateCustomer function in code behind.
protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
{
// assuming that the value of your PersonID is numeric value
long personID = long.Parse(GridView1.DataKeys[e.RowIndex].Values["PersonID"].ToString());
....
}
The personID value you can pass as an argument to your WHERE clause.
Alternatively you can declare a HiddenField in your gridview and bind PersonID which you can access in you UpdateCustomer function using FindControl method.
This question already has answers here:
SQL Server Stored Procedure Update not working with ASP.Net C# GridView RowUpdating
(3 answers)
Closed 9 years ago.
I am having an issue but I am not getting any errors thrown. What's happening is that I have a stored procedure that is driving my update but I am unable to get the gridview to update.
I hit the Update link button but it doesn't actually update the record.
Here is my stored procedure:
ALTER PROCEDURE [dbo].[CompDev_Update](
#CompID int,
#NewDevCount nchar(10) = null,
#DevelopmentName nvarchar(255) = null,
#City nvarchar(255) = null,
#State nvarchar(255) = null,
#ZipCodeofNewDev nchar(10) = null,
#ProjectStatus nvarchar(255) = null,
#ShoppingCenter nvarchar(255) = null,
#ProjectStartDate float = null,
#ProjectDescription nvarchar(255) = null,
#ProposedAnchorTenants nvarchar(255) = null,
#GLA nchar(10) = null,
#EstCompDate float = null,
#Developer nvarchar(255) = null,
#BusinessUnit nchar(10) = null,
#BU nchar(10) = null,
#CenterName nvarchar(255) = null,
#MSA nvarchar(255) = null,
#BrixmorMSARank nchar(10) = null,
#Count nchar(10) = null,
#Region nvarchar(255) = null,
#DistancefromNewDev nchar(10) = null
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE WestCompetition Set NewDevCount=#NewDevCount, DevelopmentName=#DevelopmentName,
City=#City, [State]=#State, ZipCodeofNewDev=#ZipCodeofNewDev, ProjectStatus=#ProjectStatus,
ShoppingCenter=#ShoppingCenter, ProjectStartDate=#ProjectStartDate, ProjectDescription=#ProjectDescription,
ProposedAnchorTenants=#ProposedAnchorTenants, GLA=#GLA, EstCompDate=#EstCompDate,
Developer=#Developer, BusinessUnit=#BusinessUnit, BU=#BU, CenterName=#CenterName,
MSA=#MSA, BrixmorMSARank=#BrixmorMSARank, [Count]=#Count, Region=#Region,
DistancefromNewDev=#DistancefromNewDev
WHERE CompID = #CompID
END
Here is my asp.net c# behind code:
protected void gvCompetition_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label CompID = (Label)gvCompetition.Rows[e.RowIndex].FindControl("lblCompID");
TextBox NewDevCount = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtNewDevCount");
TextBox DevelopmentName = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtDevelopmentName");
TextBox City = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtCity");
TextBox State = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtState");
TextBox ZipCodeofNewDev = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtZipCode");
TextBox ProjectStatus = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtProjectStatus");
TextBox ShoppingCenter = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtShoppingCenter");
TextBox ProjectStartDate = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtProjectStartDate");
TextBox ProjectDescription = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtProjectDescription");
TextBox ProposedAnchorTenants = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtProposedAnchorTenants");
TextBox GLA = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtGLA");
TextBox EstCompDate = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtEstCompDate");
TextBox Developer = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtDeveloper");
TextBox BusinessUnit = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtBusinessUnit");
TextBox BU = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtBU");
TextBox CenterName = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtCenterName");
TextBox MSA = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtMSA");
TextBox BrixmorMSARank = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtBrixmorMSARank");
TextBox Count = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtCount");
TextBox Region = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtRegion");
TextBox DistancefromNewDev = (TextBox)gvCompetition.Rows[e.RowIndex].FindControl("txtDistancefromNewDev");
string ecompid = CompID.Text;
string enewdevcount = NewDevCount.Text;
string edevelopmentname = DevelopmentName.Text;
string ecity = City.Text;
string estate = State.Text;
string ezipcode = ZipCodeofNewDev.Text;
string eprojectstatus = ProjectStatus.Text;
string eshoppingcenter = ShoppingCenter.Text;
string eprojectstartdate = ProjectStartDate.Text;
string eprojectdescription = ProjectDescription.Text;
string eproposedanchor = ProposedAnchorTenants.Text;
string egla = GLA.Text;
string eestcompdate = EstCompDate.Text;
string edeveloper = Developer.Text;
string ebusinessunit = BusinessUnit.Text;
string ebu = BU.Text;
string ecentername = CenterName.Text;
string emsa = MSA.Text;
string ebrixmormsarank = BrixmorMSARank.Text;
string ecount = Count.Text;
string eregion = Region.Text;
string edistancefromnewdev = DistancefromNewDev.Text;
UpdateCompetition(ecompid, enewdevcount, edevelopmentname, ecity, estate, ezipcode, eprojectstatus, eshoppingcenter, eprojectstartdate, eprojectdescription, eproposedanchor,
egla, eestcompdate, edeveloper, ebusinessunit, ebu, ecentername, emsa, ebrixmormsarank, ecount, eregion, edistancefromnewdev);
gvCompetition.EditIndex = -1;
BindCompetition();
}
protected void UpdateCompetition(string compid, string newdevcount, string developmentname, string city, string state, string zipcode,
string projectstatus, string shoppingcenter, string projectstartdate, string projectdescription, string proposedanchor, string gla,
string estcompdate, string developer, string businessunit, string bu, string centername, string msa, string brixmormsarank,
string count, string region, string distancefromnewdev)
{
SqlConnection conn = new SqlConnection(cnnString);
conn.Open();
command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "CompDev_Update";
command.Parameters.Add(new SqlParameter("#CompID", SqlDbType.Int));
command.Parameters.Add(new SqlParameter("#NewDevCount", SqlDbType.NChar, 10));
command.Parameters.Add(new SqlParameter("#DevelopmentName", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#City", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#State", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#ZipCodeofNewDev", SqlDbType.NChar, 10));
command.Parameters.Add(new SqlParameter("#ProjectStatus", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#ShoppingCenter", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#ProjectStartDate", SqlDbType.Float));
command.Parameters.Add(new SqlParameter("#ProjectDescription", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#ProposedAnchorTenants", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#GLA", SqlDbType.NChar, 10));
command.Parameters.Add(new SqlParameter("#EstCompDate", SqlDbType.Float));
command.Parameters.Add(new SqlParameter("#Developer", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#BusinessUnit", SqlDbType.NChar, 10));
command.Parameters.Add(new SqlParameter("#BU", SqlDbType.NChar, 10));
command.Parameters.Add(new SqlParameter("#CenterName", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#MSA", SqlDbType.NChar, 10));
command.Parameters.Add(new SqlParameter("#BrixmorMSARank", SqlDbType.NChar, 10));
command.Parameters.Add(new SqlParameter("#Count", SqlDbType.NChar, 10));
command.Parameters.Add(new SqlParameter("#Region", SqlDbType.NVarChar, 255));
command.Parameters.Add(new SqlParameter("#DistancefromNewDev", SqlDbType.NChar, 10));
command.Parameters["#CompID"].Value = Convert.ToInt32(compid.ToString());
command.Parameters["#NewDevCount"].Value = newdevcount;
command.Parameters["#DevelopmentName"].Value = developmentname;
command.Parameters["#City"].Value = city;
command.Parameters["#State"].Value = state;
command.Parameters["#ZipCodeofNewDev"].Value = zipcode;
command.Parameters["#ProjectStatus"].Value = projectstatus;
command.Parameters["#ShoppingCenter"].Value = shoppingcenter;
command.Parameters["#ProjectStartDate"].Value = projectstartdate;
command.Parameters["#ProjectDescription"].Value = projectdescription;
command.Parameters["#ProposedAnchorTenants"].Value = proposedanchor;
command.Parameters["#GLA"].Value = gla;
command.Parameters["#EstCompDate"].Value = estcompdate;
command.Parameters["#Developer"].Value = developer;
command.Parameters["#BusinessUnit"].Value = businessunit;
command.Parameters["#BU"].Value = bu;
command.Parameters["#CenterName"].Value = centername;
command.Parameters["#MSA"].Value = msa;
command.Parameters["#BrixmorMSARank"].Value = brixmormsarank;
command.Parameters["#Count"].Value = count;
command.Parameters["#Region"].Value = region;
command.Parameters["#DistancefromNewDev"].Value = distancefromnewdev;
da = new SqlDataAdapter(command);
ds = new DataSet();
da.Fill(ds);
conn.Close();
}
Here is my asp.net code:
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvCompetition" OnPageIndexChanging="gvCompetition_PageIndexChanging"
OnRowEditing="gvCompetition_RowEditing" OnRowCancelingEdit="gvCompetition_RowCancelingEdit"
OnRowUpdating="gvCompetition_RowUpdating"
runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None"
BorderWidth="1px" CellPadding="1" ForeColor="Black" GridLines="Vertical"
AllowPaging="True" AutoGenerateColumns="False" Font-Size="10px">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#7a1501" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#ed2d22" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#f68121" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
<Columns>
<asp:templatefield Headertext="ID">
<itemtemplate>
<asp:label ID="lblCompID" runat="server" text='<%#Eval("CompID") %>'></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:TemplateField HeaderText="New Dev Count">
<ItemTemplate>
<asp:Label ID="lblNewDevCount" runat="server" Text='<%#Eval("NewDevCount") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNewDevCount" Width="100%" runat="server" Text='<%#Eval("NewDevCount") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Development Name">
<ItemTemplate>
<asp:Label ID="lblDevelopmentName" runat="server" Text='<%#Eval("DevelopmentName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDevelopmentName" Width="100%" runat="server" Text='<%#Eval("DevelopmentName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCity" Width="100%" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="lblState" runat="server" Text='<%#Eval("State") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtState" Width="100%" runat="server" Text='<%#Eval("State") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Zip Code">
<ItemTemplate>
<asp:Label ID="lblZipCode" runat="server" Text='<%#Eval("ZipCodeofNewDev") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtZipCode" Width="100%" runat="server" Text='<%#Eval("ZipCodeofNewDev") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Status">
<ItemTemplate>
<asp:Label ID="lblProjectStatus" runat="server" Text='<%#Eval("ProjectStatus") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProjectStatus" Width="100%" runat="server" Text='<%#Eval("ProjectStatus") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shopping Center">
<ItemTemplate>
<asp:Label ID="lblShoppingCenter" runat="server" Text='<%#Eval("ShoppingCenter") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtShoppingCenter" Width="100%" runat="server" Text='<%#Eval("ShoppingCenter") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Start Date">
<ItemTemplate>
<asp:Label ID="lblProjectStartDate" runat="server" Text='<%#Eval("ProjectStartDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProjectStartDate" Width="100%" runat="server" Text='<%#Eval("ProjectStartDate") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Description">
<ItemTemplate>
<asp:Label ID="lblProjectDescription" runat="server" Text='<%#Eval("ProjectDescription") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProjectDescription" Width="100%" runat="server" Text='<%#Eval("ProjectDescription") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Proposed Anchor Tenants">
<ItemTemplate>
<asp:Label ID="lblProposedAnchorTenants" runat="server" Text='<%#Eval("ProposedAnchorTenants") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProposedAnchorTenants" Width="100%" runat="server" Text='<%#Eval("ProposedAnchorTenants") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GLA">
<ItemTemplate>
<asp:Label ID="lblGLA" runat="server" Text='<%#Eval("GLA") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtGLA" Width="100%" runat="server" Text='<%#Eval("GLA") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Est. Completion Date">
<ItemTemplate>
<asp:Label ID="lblEstCompDate" runat="server" Text='<%#Eval("EstCompDate") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEstCompDate" Width="100%" runat="server" Text='<%#Eval("EstCompDate") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Developer">
<ItemTemplate>
<asp:Label ID="lblDeveloper" runat="server" Text='<%#Eval("Developer") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDeveloper" Width="100%" runat="server" Text='<%#Eval("Developer") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Business Unit">
<ItemTemplate>
<asp:Label ID="lblBusinessUnit" runat="server" Text='<%#Eval("BusinessUnit") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBusinessUnit" Width="100%" runat="server" Text='<%#Eval("BusinessUnit") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BU">
<ItemTemplate>
<asp:Label ID="lblBU" runat="server" Text='<%#Eval("BU") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBU" Width="100%" runat="server" Text='<%#Eval("BU") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CenterName">
<ItemTemplate>
<asp:Label ID="lblCenterName" runat="server" Text='<%#Eval("CenterName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCenterName" Width="100%" runat="server" Text='<%#Eval("CenterName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MSA">
<ItemTemplate>
<asp:Label ID="lblMSA" runat="server" Text='<%#Eval("MSA") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMSA" Width="100%" runat="server" Text='<%#Eval("MSA") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brixmor MSA Rank">
<ItemTemplate>
<asp:Label ID="lblBrixmorMSARank" runat="server" Text='<%#Eval("BrixmorMSARank") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtBrixmorMSARank" Width="100%" runat="server" Text='<%#Eval("BrixmorMSARank") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:Label ID="lblCount" runat="server" Text='<%#Eval("Count") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCount" Width="100%" runat="server" Text='<%#Eval("Count") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Region">
<ItemTemplate>
<asp:Label ID="lblRegion" runat="server" Text='<%#Eval("Region") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtRegion" Width="100%" runat="server" Text='<%#Eval("Region") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Distance from New Dev">
<ItemTemplate>
<asp:Label ID="lblDistancefromNewDev" runat="server" Text='<%#Eval("DistancefromNewDev") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDistancefromNewDev" Width="100%" runat="server" Text='<%#Eval("DistancefromNewDev") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" Text="EDIT" runat="server" CommandName="Edit" />
<span onclick="return confirm('Are you sure you want to delete this record')">
<asp:LinkButton ID="btnDelete" Text="DELETE" runat="server" CommandName="Delete" />
</span>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" Text="UPDATE" runat="server" CommandName="Update" />
<br />
<asp:LinkButton ID="btnCancel" Text="CANCEL" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvCompetition" EventName="PageIndexChanging" />
<asp:AsyncPostBackTrigger ControlID="gvCompetition" EventName="RowEditing" />
<asp:AsyncPostBackTrigger ControlID="gvCompetition" EventName="RowUpdating" />
<asp:AsyncPostBackTrigger ControlID="gvCompetition" EventName="RowCancelingEdit" />
</Triggers>
</asp:UpdatePanel>
What am I doing wrong that the update will not happen? And why am I not getting any errors thrown when it does not update?
Because you are not executing your query.
Try with
command.ExecuteNonQuery();
or you need to set the UpdateCommand property of DataAdapter
da.UpdateCommand = command;