I have a gridview and has only three columns and I am trying to passID to stored procedure. Once stored procedure receives theID, it will insert to data to different table in different database. I all need is how to pass the value ofID to stored procedure onclick button. I am really stuck and I couldnt figure it out.
Here is my class
using System;
using System.Configuration;
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.Data.SqlTypes;
namespace SpecialmL
{
public partial class Specialmail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ClientsConnectionString"].ConnectionString);
SqlCommand comm = new SqlCommand();
comm.CommandText = "sph_NonProfit_ReconMail_SingleCampaign2"; // stored procedure
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
Button btn = (Button)sender;
string CommandName = btn.CommandName;
string CommandArgument = btn.CommandArgument;
}
}
}
and Here is my aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Specialmail.aspx.cs" Inherits="SpecialmL.Specialmail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
// Display the first name from the selected row.
// In this example, the third column (index 2) contains
// the first name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}
void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{
// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
// You can cancel the select operation by using the Cancel
// property. For this example, if the user selects a customer with
// the ID "ANATR", the select operation is canceled and an error message
// is displayed.
if (row.Cells[1].Text == "ANATR")
{
e.Cancel = true;
MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView Select Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>GridView Select Example</h3>
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSource"
autogeneratecolumns="False"
autogenerateselectbutton="True"
selectedindex="1"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged"
onselectedindexchanging="CustomersGridView_SelectedIndexChanging"
runat="server" DataKeyNames="ID">
<Columns>
<asp:BoundField DataField="ID"
HeaderText="ID"
InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="CustomData"
HeaderText="CustomData"
SortExpression="CustomData" />
<asp:BoundField DataField="Name"
HeaderText="Name"
SortExpression="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server" CommandName="AddToCart"
CommandArgument = "Button1" OnClick = "Button1_Click"
Text='Submit'
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<selectedrowstyle backcolor="LightCyan"
forecolor="DarkBlue"
font-bold="true"/>
</asp:gridview>
<br/>
<asp:label id="MessageLabel"
forecolor="Red"
runat="server"/>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="CustomersSource"
SelectCommand="select customdata, name,
ID from asdm.dbo.Campaigns where ExtraInfo = 'NPT'"
ConnectionString='<%$ ConnectionStrings:ASDMConnectionString %>' runat="server" />
</form>
</body>
</html>