Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Jobs_Accounting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                Userid.Value = Session["ID"].ToString();
            }
            catch
            {
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int s1 = GridView1.SelectedIndex;

        string ss = GridView1.Rows[s1].Cells[1].Text;

        SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString);
        a.Open();

        string cartCmd = "insert into savetable(userid,productid,Quantity) values('" + Userid.Value + "','" + ss + "','" + 1 + "') ";

        SqlCommand cmd = new SqlCommand(cartCmd, a);
        cmd.ExecuteNonQuery();
        a.Close();
    }
}

in the above code once select button control go to the gridviewrow----------line

but not go to the selected changed event why please reply me
Posted
Updated 24-Feb-14 1:07am
v2
Comments
member1431 24-Feb-14 7:18am    
error:

Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'.

{
Line 47:
Line 48: GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
Line 49: int rowIndex = row.RowIndex;

this is error

1 solution

Try like this:
C#
protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
      Session["CheckRefresh"]= Server.UrlDecode(System.DateTime.Now.ToString());
     }
 }

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (Session["CheckRefresh"].ToString()==ViewState["CheckRefresh"].ToString())
     {
         int s1 = GridView1.SelectedIndex;

         string ss = GridView1.Rows[s1].Cells[1].Text;

    SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString);
         a.Open();

         string cartCmd = "insert into savetable(userid,productid,Quantity) values('" + Userid.Value + "','" + ss + "','" + 1 + "') ";

         SqlCommand cmd = new SqlCommand(cartCmd, a);
         cmd.ExecuteNonQuery();
         a.Close();
  Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
     }
 }
 
Share this answer
 
Comments
member1431 24-Feb-14 7:15am    
Dear Jas,

I got same error once click the button that control goes to gridview row----------line

ok jas leave it i will try later
thanks for your reply jas
Tom Marvolo Riddle 24-Feb-14 7:17am    
what error you are getting? try it and let me know.It should work
Tom Marvolo Riddle 24-Feb-14 7:29am    
post your html markup
member1431 24-Feb-14 7:32am    
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
Culture="en-IN" CodeFile="MobilePhones-ComputersSoftware.aspx.cs" Inherits="Styles_MobilePhones_ComputersSoftware" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="100%" AutoGenerateColumns="False"
DataKeyNames="Id" DataSourceID="SqlDataSource1" HeaderStyle-HorizontalAlign="Justify"
GridLines="Horizontal" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
OnRowCommand="GridView1_RowCommand" onprerender="GridView1_PreRender">
<columns>
<asp:CommandField ShowSelectButton="True" Visible="False" ButtonType="Button" />
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True"
SortExpression="Id" />
<asp:TemplateField HeaderText="Image" SortExpression="Image">
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Image") %>'>

<itemtemplate>
<asp:Image ID="Image1" runat="server" Width="100px" Height="100px" ImageUrl='<%# Bind("Image") %>'>



<asp:TemplateField HeaderText="Title" SortExpression="Title">
<edititemtemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Title") %>'>

<itemtemplate>
<asp:LinkButton ID="LinkButton2" runat="server" Text='<%# Bind("Title") %>'>


<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" DataFormatString="{0:C}" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
<asp:BoundField DataField="Mobile" HeaderText="Mobile" SortExpression="Mobile" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
<asp:BoundField DataField="DateAdded" HeaderText="DateAdded" SortExpression="DateAdded" />
<asp:CommandField ShowSelectButton="True" SelectText="Save" ButtonType="Button" />


<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:abcConnectionString %>"
SelectCommand="SELECT [Id], [Image], [Title], [Description], [Price], [Location], [Email], [Mobile], [ContactName], [DateAdded] FROM [ProductDetails] where CategoriesType='Computer & Software'">

<asp:Label ID="Label1" runat="server" Text="Label">
</div>
<asp:HiddenField ID="Userid" runat="server" />
Tom Marvolo Riddle 24-Feb-14 7:37am    
IN .cs page where is the rowcommand event

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900