<script>
function validate() {
var editName = document.getElementById('<%=txt_editName.ClientID %>').value;
var editPrice = document.getElementById('<%=txt_editPrice.ClientID %>').value;
if (editName == "") {
alert("Enter Name");
return false;
}
if (editPrice == "" && editPrice.value != 0) {
alert("Enter Price");
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table class="auto-style1">
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txt_serchName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Price</td>
<td>
<asp:TextBox ID="txt_SearchPrice" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btn_search" runat="server" Text="Search" OnClick="btn_search_Click" />
</td>
</tr>
</table>
<br />
Add/Edit:<br />
<table class="auto-style1">
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txt_editName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Price</td>
<td>
<asp:TextBox ID="txt_editPrice" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button ID="btn_saveItems" OnClientClick="return validate()" runat="server" Text="Save" OnClick="Button1_Click" />
<br />
<asp:Label ID="lbl_success" runat="server"></asp:Label>
</td>
</tr>
</table>
<p>
</p>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="id" HeaderText="S.No." />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:BoundField DataField="price" HeaderText="price" />
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
i want to make webservice that i can update the price at client side with jquery and webservice .ability to edit the price inline (clientside using jquery and webservice)
public partial class Items_Page : System.Web.UI.Page
{ String conctiioon = ConfigurationManager.ConnectionStrings["shopcart"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvbind();
}
}
protected void btn_search_Click(object sender, EventArgs e)
{
try
{
String strConnString = ConfigurationManager.ConnectionStrings["shopcart"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
if (txt_serchName.Text == "" && txt_SearchPrice.Text == "")
{
SqlCommand comm = new SqlCommand();
comm.CommandText = "Select * from items";
comm.Connection = con;
GridView2.DataSource = comm.ExecuteReader();
GridView2.DataBind();
}
if (txt_serchName.Text.Length > 0 && txt_SearchPrice.Text.Length > 0)
{
SqlCommand com = new SqlCommand();
com.CommandText = "SearchItemByName_Price";
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.Parameters.Add(new SqlParameter("@name", txt_serchName.Text));
GridView2.EmptyDataText = "No Records Found";
GridView2.DataSource = com.ExecuteReader();
GridView2.DataBind();
}
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlTransaction tran = null;
try
{
String strConnString = ConfigurationManager.ConnectionStrings["shopcart"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
using (SqlCommand com = new SqlCommand())
{
com.CommandText = "Insert_EditItems";
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.Parameters.Add(new SqlParameter("@name", txt_editName.Text));
com.Parameters.Add(new SqlParameter("@Price", txt_editPrice.Text));
con.Open();
tran = con.BeginTransaction();
com.Transaction = tran;
com.ExecuteNonQuery();
tran.Commit();
lbl_success.Text = "Record inserted successfully";
}
SqlCommand com_ = new SqlCommand();
com_.CommandText = "Select * from items";
com_.Connection = con;
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = com_;
sda.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
con.Close();
}
catch (Exception ex)
{
tran.Rollback();
lbl_success.Text = "You Can not save this item ";
}
if (Session["rowid"]!=null)
{
}
}
protected void gvbind()
{
String conction = ConfigurationManager.ConnectionStrings["shopcart"].ConnectionString;
SqlConnection con = new SqlConnection(conction);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from items";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView2.DataSource = ds;
GridView2.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView2.DataSource = ds;
GridView2.DataBind();
int columncount = GridView2.Rows[0].Cells.Count;
GridView2.Rows[0].Cells.Clear();
GridView2.Rows[0].Cells.Add(new TableCell());
GridView2.Rows[0].Cells[0].ColumnSpan = columncount;
GridView2.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void displayRowIndex()
{
//int rowid= Session["rowid"].ToString();
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex = -1;
gvbind();
}
//protected void rowid(int id)
//{
// SqlConnection con = new SqlConnection(conctiioon);
// SqlCommand cmd = new SqlCommand("Select * from items where id ="+id,con);
// cmd.Connection = con;
// cmd.ExecuteReader();
//}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
//Session["rowid"] = e.NewEditIndex;
//int idd=int.Parse( Session["rowid"].ToString());
//rowid(idd);
gvbind();
}
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = new SqlConnection(conctiioon);
int userid = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
//TextBox textName = (TextBox)row.Cells[0].Controls[0];
TextBox textName = (TextBox)row.Cells[1].Controls[0];
TextBox textprice = (TextBox)row.Cells[2].Controls[0];
GridView2.EditIndex = -1;
conn.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
SqlCommand cmd = new SqlCommand("update items set name='" + textName.Text + "',price='" + textprice.Text + "' where id='" + userid + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection(conctiioon);
GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
conn.Open();
SqlCommand cmd = new SqlCommand("delete FROM items where id='" + Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}
}