I have a gridview like below:
<asp:GridView ID="gvitems" runat="server" AutoGenerateColumns="false" CssClass="GridStyle" AllowSorting="true" OnSorting="OnSorting" DataKeyNames="Id" OnRowCommand="gv_RowCommand"
OnRowEditing="gv_RowEditing" OnRowUpdating="gv_RowUpdating" OnRowCancelingEdit="gv_RowCancelEdit" >
<Columns>
<asp:buttonfield buttontype="Link"
commandname="View"
text="View"/>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button runat="server" Text="Update" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Next Steps">
<ItemTemplate>
<asp:Label runat="server" ID="lblNextSteps" Text='<%# Eval("NextSteps")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtNextSteps" Text='<%# Eval("NextSteps")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="txtNextStepsFooter"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And in the backend, this is how i fill it with data:
DataSet dataSet = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid_SubmittedProjects();
}
}
private void BindGrid_SubmittedProjects(string sortExpression = null)
{
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ProjectsDataBase"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("ViewProjects", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#UserName", SqlDbType.VarChar).Value = "myusername";
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
dataSet = new DataSet();
sda.Fill(dataSet, "Project");
}
gvitems.DataSource = dataSet.Tables[0].DefaultView;
gvitems.DataBind();
}
}
}
When I click on the "View" hyperlink on each row, it works, so OnRowCommand function below is triggered.
protected void gv_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "View")
{
//my work
}
}
However, when I click on "Edit" button, nothing is happening, OnRowEditing is not being triggered.
protected void gv_RowEditing(Object sender, GridViewEditEventArgs e)
{
gvitems.EditIndex = e.NewEditIndex;
this.BindGrid_SubmittedProjects();
}
protected void gv_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
//my update query.
}
I could not see what I am doing wrong. Tried many things I've found over online but none worked. Any help would be so appreciated.
Regards.
Using LinkButton instead of regular button fixed the issue.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
<asp:LinkButton Text="Delete" runat="server" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton Text="Update" runat="server" CommandName="Update" />
<asp:LinkButton Text="Cancel" runat="server" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
I am creating a system in which i want to edit several records from the database through administrator using a data grid view:
Table Schema used in this is
CREATE TABLE [dbo].[tbl_Employee] (
[ID] INT NOT NULL,
[Name] VARCHAR (50) NULL,
[City] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([ID] ASC)
);
I have added Add OnRowEditing, OnRowUpdating and OnRowCancelingEdit events to the GridView.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lbl_City" runat="server" Text='<%#Eval("City") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_City" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>
<RowStyle BackColor="#e7ceb6"/>
</asp:GridView>
</div>
And the c# code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
//Connection String from web.config File
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
SqlConnection con;
SqlDataAdapter adapt;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ShowData();
}
}
//ShowData method for Displaying Data in Gridview
protected void ShowData()
{
dt = new DataTable();
con = new SqlConnection(cs);
con.Open();
adapt = new SqlDataAdapter("Select ID,Name,City from tbl_Employee",con);
adapt.Fill(dt);
if(dt.Rows.Count>0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
{
//NewEditIndex property used to determine the index of the row being edited.
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)
{
//Finding the controls from Gridview for the row which is going to update
Label id=GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;
TextBox name = GridView1.Rows[e.RowIndex].FindControl("txt_Name") as TextBox;
TextBox city = GridView1.Rows[e.RowIndex].FindControl("txt_City") as TextBox;
con = new SqlConnection(cs);
con.Open();
//updating the record
SqlCommand cmd = new SqlCommand("Update tbl_Employee set Name='"+name.Text+"',City='"+city.Text+"' where ID="+Convert.ToInt32(id.Text),con);
cmd.ExecuteNonQuery();
con.Close();
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
//Call ShowData method for displaying updated data
ShowData();
}
protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
{
//Setting the EditIndex property to -1 to cancel the Edit mode in Gridview
GridView1.EditIndex = -1;
ShowData();
}
}
The code is not showing any error but the database is not getting updated.
Where am i going wrong?
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
I want to update only selected row from my grid view when i press linked button but it is not working on my end .
Here is my Design Page
<asp:GridView ID="grdCompanyUsers" runat="server"
DataKeyNames="id_company_user,nm_company_username"
AutoGenerateColumns="false" GridLines="None" CssClass="grid" AlternatingRowStyle-
BackColor="#DDE0EF" OnRowDataBound="grdCompanyUsers_DataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images
/Cancel.jpg" ToolTip="Cancel" Height="20px" Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CompUserID" runat="server" Width="15"
Text='<%#Eval("id_company_user")%>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="companyusername" runat="server" Width="51"
Text='<%#Eval("nm_company_username")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="compName" runat="server" Width="56" Text='<%#Eval("nm_company_name")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="compDesc" runat="server" Width="129" Text='<%#Eval("nm_company_desc")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="compEmail" runat="server" Width="103px"
Text='<%#Eval("nm_company_email_address")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="compAddress" runat="server" Width="153px"
Text='<%#Eval("nm_company_address")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkBoxStatus" runat="server" Width="15px" Enabled="false"
Text='<%#Eval("ind_active")%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" Font-Underline="false"
CommandArgument='<%#Eval ("id_company_user")%>'
OnClick="btnEdit_Click">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkDeny" Font-Underline="false" CommandName="Deny"
CommandArgument='<%# Eval("id_company_user") %>'
OnClick="btnDeny_Click">Deny</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is my code behind the aspx page
protected void btnEdit_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
int i = Convert.ToInt32(row.RowIndex);
_connString = ConfigurationManager.AppSettings["connString"];
using (SqlConnection conn = new SqlConnection(_connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("update ref_registration_company_user
set ind_active=1 where id_company_user=id_company_user", conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
Here is Design View :
I just want that only selected row should be affected in database.
I will be thank full for help.
you need to set the id_company_user value in your sql statement. use parameters as below.
SqlCommand cmd = new SqlCommand("update ref_registration_company_user set ind_active=1 where id_company_user=#id_company_user", conn);
cmd.Parameters.AddWithValue("#id_company_user", id);
you need to get current row id_company_user value check below SO question and answer, you can use
OnRowCommand of GridView and CommandArgument property
GridView: Get datakey of the row on button click
<asp:GridView ID="grdCompanyUsers" runat="server" DataKeyNames="id_company_user,nm_company_username" AutoGenerateColumns="false" GridLines="None" CssClass="grid" AlternatingRowStyle-BackColor="#DDE0EF" OnRowDataBound="grdCompanyUsers_DataBound"
OnRowCommand="myGridView_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" Font-Underline="false" CommandArgument='<%#Eval ("id_company_user")%>' CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
code-behind:
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
var id= int.Parse(e.CommandArgument);
_connString = ConfigurationManager.AppSettings["connString"];
using (SqlConnection conn = new SqlConnection(_connString))
{
conn.Open();
using(SqlCommand cmd = new SqlCommand("update ref_registration_company_user set ind_active=1 where id_company_user=id_company_user", conn))
{
cmd.Parameters.AddWithValue("#id_company_user", id);
cmd.ExecuteNonQuery();
}
}
}
Can you please check once your update query where clause,
Check the sqlDataSource control for this problem, you can add the select and update command for populate and update rows your GridView on there.
Maybe this example could be you useful
http://asp-net-example.blogspot.mx/2008/12/aspnet-gridview-and-sqldatasource.html
I am doing insert/update and delete in gridview. For that I am using ItemTemplate which contains labels to show the values. But when the gridview is in edit mode, the dropdown lists comes in place of that labels. I want to set the selected values of drop down lists to the values of labels. My drop down lists dont have datasource. I am binding dropdown list from 0 to 99. Below is the code for my edit method.
protected void grdUsedCatheters_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
grdUsedCatheters.EditIndex = e.NewEditIndex;
BindCatheterGrid();
DropDownList ddlFrom = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddFrom");
DropDownList ddlTo = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddTo");
BindDropDowns(ddlFrom);
BindDropDowns(ddlTo);
}
catch (Exception ex)
{
if (ex.HelpLink == null)
lblMessage.Text = ex.Message;
else
lblMessage.Text = ex.HelpLink;
lblMessage.CssClass = "ERROR";
}
private void BindDropDowns(DropDownList ddl)
{
for (int i = 0; i <= 99; i++)
ddl.Items.Add(i.ToString());
}
below is the part of markup of my gridview
<asp:TemplateField HeaderText="Cine Run">
<ItemTemplate>
From: <asp:Label ID="lblFrom" runat="server" ><%# Eval("CineRunFrom")%></asp:Label>
To: <asp:Label ID="lblTo" runat="server"><%# Eval("CineRunTo")%></asp:Label>
</ItemTemplate>
<EditItemTemplate>
From: <asp:DropDownList ID="ddFrom" runat="server" Width="50px">
</asp:DropDownList>
To: <asp:DropDownList ID="ddTo" runat="server" Width="50px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
From: <asp:DropDownList ID="ddFromF" runat="server" Width="50px"> </asp:DropDownList>
To: <asp:DropDownList ID="ddToF" runat="server" Width="50px"> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
}
Retrieve the values of label's before setting grdUsedCatheters.EditIndex = e.NewEditIndex and calling BindCatheterGrid() method and then after populating the DropDownLists set their selected value accordingly. Like this:
protected void grdUsedCatheters_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
Label lblFrom = (Label)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("lblFrom"); //lblFrom is the ID of label
grdUsedCatheters.EditIndex = e.NewEditIndex;
BindCatheterGrid();
DropDownList ddlFrom = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddFrom");
DropDownList ddlTo = (DropDownList)grdUsedCatheters.Rows[e.NewEditIndex].FindControl("ddTo");
BindDropDowns(ddlFrom);
BindDropDowns(ddlTo);
ddlFrom.Text = lblFrom.Text;
}
catch (Exception ex)
{
if (ex.HelpLink == null)
lblMessage.Text = ex.Message;
else
lblMessage.Text = ex.HelpLink;
lblMessage.CssClass = "ERROR";
}
}
Edit
and also change your gridview markup like this:
<asp:TemplateField HeaderText="Cine Run">
<ItemTemplate>
From: <asp:Label ID="lblFrom" runat="server" Text='<%# Eval("CineRunFrom")%>' />
To: <asp:Label ID="lblTo" runat="server" Text='<%# Eval("CineRunTo")%>' />
</ItemTemplate>
...
I think this example will work for you.
First you put hidden field in EditItemTemplate where u have put the Dropdownlist.
Set the value of hidden field as you set the value of label in ItemTemplate
See my code:
<asp:GridView runat="server" ID="gridExample" OnRowEditing="gridExample_RowEditing"
AutoGenerateEditButton="True" AutoGenerateColumns ="false" OnRowCancelingEdit ="gridExample_RowCancelingEdit" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblID" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="drpName">
</asp:DropDownList>
<asp:HiddenField runat ="server" ID ="hdnId" Value ='<%# Eval("ID") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lblName" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:TextBox runat ="server" ID="txtName" Text ='<%# Eval("Name") %>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void gridExample_RowEditing(object sender, GridViewEditEventArgs e)
{
gridExample.EditIndex = e.NewEditIndex;
BindGrid();
DropDownList dl=new DropDownList ();
dl = (DropDownList)gridExample.Rows[gridExample.EditIndex].FindControl("drpName");
FillDrops(dl);
HiddenField hdnId = new HiddenField();
hdnId = (HiddenField)gridExample.Rows[gridExample.EditIndex].FindControl("hdnId");
dl.Text = hdnId.Value;
}