Hi all,
I have my grid view and its code is as follows
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="NewCampaign.aspx.cs" Inherits="NoticeBoard.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">
<title></title> <style type="text/css"> .style1
{ width: 160px; } .style2 {
width: 176px; } .style3 {
width: 160px; height: 23px; } .style4
{ width: 176px; height: 23px; }
.style5 { height: 23px; } </style>
</head>
<body style="height: 551px; width: 797px" bgcolor="White">
<form id="form1" runat="server"> <div>
<asp:GridView ID="noticeDetails" runat="server" BorderWidth="1px" AutoGenerateColumns="false" PageSize="10" Width="10%" CellPadding="10"
AllowSorting="true" AllowPaging="true" AutoGenerateEditButton="true" onrowediting="noticeDetails_RowEditing" OnRowUpdating="UpdateRecord">
<Columns>
<asp:TemplateField HeaderText="CampaignIDRange">
<ItemTemplate> <%# Eval("CampaignIDRange")%>
</ItemTemplate> <EditItemTemplate>
<asp:TextBox runat="server" ID="txtCampaignIDRange" Text='<%# Eval("CampaignIDRange")%>' />
</EditItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="PromoStartDate">
<ItemTemplate> <%# Eval("PromoStartDate")%>
</ItemTemplate> <EditItemTemplate>
<asp:TextBox runat="server" ID="txtPromoStartDate" Text='<%# Eval("PromoStartDate")%>' />
</EditItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="PromoEndDate">
<ItemTemplate> <%# Eval("PromoEndDate")%>
</ItemTemplate> <EditItemTemplate>
<asp:TextBox runat="server" ID="txtPromoEndDate" Text='<%# Eval("PromoEndDate")%>' />
</EditItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="ExecutionDate">
<ItemTemplate> <%# Eval("ExecutionDate")%>
</ItemTemplate> <EditItemTemplate>
<asp:TextBox runat="server" ID="txtExecutionDate" Text='<%# Eval("ExecutionDate")%>' />
</EditItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="ExpiryDate">
<ItemTemplate> <%# Eval("ExpiryDate")%>
</ItemTemplate> <EditItemTemplate>
<asp:TextBox runat="server" ID="txtExpiryDate" Text='<%# Eval("ExpiryDate")%>' />
</EditItemTemplate> </asp:TemplateField>
</Columns> </asp:GridView> </div>
<table style="width:100%;" runat ="server" id="myform">
<tr> <td class="style1">
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="true"
Text="Campaign ID Range" oncheckedchanged="RadioButton1_CheckedChanged" />
</td> <td class="style2">
<asp:TextBox ID="Txtadd5" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text="to"></asp:Label>
</td> <td>
<asp:TextBox ID="Txtadd6" runat="server"></asp:TextBox>
</td> </tr> <tr>
<td class="style1">
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="true" Text="Manual Entry"
oncheckedchanged="RadioButton2_CheckedChanged" />
</td>
<td class="style2">
<asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
</td> <td>
</td> </tr>
<tr> <td class="style1">
</td>
<td class="style2">
</td> <td>
</td> </tr>
<tr> <td class="style1">
<asp:Label ID="Label1" runat="server" Text="Promo Start Date"></asp:Label>
</td> <td class="style2">
<asp:TextBox ID="txtadd1" runat="server" ></asp:TextBox>
</td> <td>
</td> </tr>
<tr> <td class="style1">
<asp:Label ID="Label2" runat="server" Text="Promo End Date"></asp:Label>
</td> <td class="style2">
<asp:TextBox ID="txtadd2" runat="server"></asp:TextBox>
</td> <td>
</td> </tr>
<tr> <td class="style3">
<asp:Label ID="Label3" runat="server" Text="Execution Date"></asp:Label>
</td> <td class="style4">
<asp:TextBox ID="txtadd3" runat="server"></asp:TextBox>
</td> <td class="style5">
</td> </tr> <tr>
<td class="style1">
<asp:Label ID="Label4" runat="server" Text="Expiry Date"></asp:Label>
</td> <td class="style2">
<asp:TextBox ID="txtadd4" runat="server"></asp:TextBox>
</td> <td>
</td> </tr>
<tr> <td class="style1">
</td>
<td class="style2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click1"
Text="Submit" /> </td>
<td> </td>
</tr> </table> <p> </p>
</form></body></html>
the aspx.cs code
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.Odbc;using System.Data;
using System.Drawing;using System.Collections; using System.Web.Security;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
namespace NoticeBoard{ public partial class WebForm1 : System.Web.UI.Page
{ DataTable dt;
protected void Page_Load(object sender, EventArgs e) {
string _connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
dt = new DataTable("tblTest");
DataColumn dc1 = new DataColumn();
dc1.DataType = typeof(String);
dc1.ColumnName = "PromoStartDate";
DataColumn dc2 = new DataColumn();
dc2.DataType = typeof(String);
dc2.ColumnName = "PromoEndDate";
DataColumn dc3 = new DataColumn();
dc3.DataType = typeof(String);
dc3.ColumnName = "ExecutionDate";
DataColumn dc4 = new DataColumn();
dc4.DataType = typeof(String);
dc4.ColumnName = "ExpiryDate";
DataColumn dc5 = new DataColumn();
dc5.DataType = typeof(String);
dc5.ColumnName = "CampaignIDRange"; dt.Columns.Add(dc1);
dt.Columns.Add(dc2); dt.Columns.Add(dc3);
dt.Columns.Add(dc4); dt.Columns.Add(dc5);
if (!Page.IsPostBack) { BindData();
} } private void BindData() {
noticeDetails.DataSource = dt;
noticeDetails.DataBind(); }
protected void noticeDetails_RowEditing(object sender, GridViewEditEventArgs e)
{ noticeDetails.EditIndex = e.NewEditIndex;
myform.Visible = false;
ViewState.Add("editRowIndex", e.NewEditIndex); }
protected void noticeDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{ e.Cancel = true; //noticeDetails.EditIndex = -1; BindData(); }
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e){
int cid = Int32.Parse(noticeDetails.DataKeys[e.RowIndex].Value.ToString()); // PUT VALUE THAT IS DEFINED AS A DATAKEY
GridViewRow grow = noticeDetails.Rows[e.RowIndex];
TextBox txtID = (TextBox)grow.FindControl("txtCampaignIDRange");
TextBox txtS = (TextBox)grow.FindControl("txtPromoStartDate");
TextBox txtE = (TextBox)grow.FindControl("txtPromoEndDate");
TextBox txtEx = (TextBox)grow.FindControl("txtExecutionDate");
TextBox txtEp = (TextBox)grow.FindControl("txtExpiryDate"); try {
OdbcConnection con = new OdbcConnection(@"Dsn=TD;uid=abc;pwd=aaa;Type=Teradata;database=dp_tmp;");
con.Open();
OdbcCommand cmd = new OdbcCommand("UpdateRecord", con);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.CommandText = "UpdateRecord";
cmd.Parameters.Add("@CampaignIDRange", ID);
cmd.Parameters.Add("@PromoStartDate", txtS.Text);
cmd.Parameters.Add("@PromoEndDate", txtE.Text);
cmd.Parameters.Add("@ExecutionDate", txtEx.Text);
cmd.Parameters.Add("@ExpiryDate", txtEp.Text);
//cmd.Connection = con; cmd.ExecuteNonQuery();
cmd.Dispose(); con.Close();
//lblmsg.Text = "Record has been Updated..!!"; }
catch (Exception ex) { //lblmsg.Text = ex.Message.ToString(); }
finally { noticeDetails.EditIndex = -1; fillgrid(); }}
public void fillgrid(){ //OdbcConnection con; //con.Open();
string Odbc = "select *from CampaignIDRange,PromoStartDate,PromoEndDate,ExecutionDate,ExpiryDate";
OdbcCommand cmd= new OdbcCommand(Odbc); dt = new DataTable();
OdbcDataAdapter da; da = new OdbcDataAdapter(cmd); da.Fill(dt);
noticeDetails.DataSource = dt; noticeDetails.DataBind();
cmd.Dispose(); //con.Close();}
protected void TextBox3_TextChanged(object sender, EventArgs e) {
} protected void TextBox1_TextChanged(object sender, EventArgs e)
{ }
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{ if (RadioButton1.Checked == true) {
RadioButton2.Enabled = false; } else
{ RadioButton2.Enabled = true; } }
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{ if (RadioButton2.Checked == true) {
RadioButton1.Enabled = false; } else
{ RadioButton1.Enabled = true; } }
protected void Button1_Click1(object sender, EventArgs e) {
{ Session["PromoStartDate"] += txtadd1.Text + "|";
Session["PromoEndDate"] += txtadd2.Text + "|";
Session["ExecutionDate"] += txtadd3.Text + "|";
Session["ExpiryDate"] += txtadd4.Text + "|";
if (RadioButton1.Checked) {
Session["CampaignIDRange"] += Txtadd5.Text + "|";
Session["CampaignIDRange2"] += Txtadd6.Text + "|";
} else {
Session["CampaignIDRange"] += TextBox7.Text + "|";
Session["CampaignIDRange2"] += TextBox7.Text + "|";
} CreateTable(); } }
public void CreateTable() {
string[] sa = Session["PromoStartDate"].ToString().Split('|');
string[] sb = Session["PromoEndDate"].ToString().Split('|');
string[] sc = Session["ExecutionDate"].ToString().Split('|');
string[] sd = Session["ExpiryDate"].ToString().Split('|');
string[] se = Session["CampaignIDRange"].ToString().Split('|');
string[] sf = Session["CampaignIDRange2"].ToString().Split('|');
int recordnum = sa.Length;
DataTable dTable = new DataTable();
DataColumn auto = new DataColumn("Index", typeof(System.Int32));
dTable.Columns.Add(auto);auto.AutoIncrement = true;auto.AutoIncrementSeed = 1;
auto.ReadOnly = true; for (int j = 0; j < recordnum - 1; j++)
{ DataRow dr;
for (int k = Convert.ToInt32(se[j]); k <= Convert.ToInt32(sf[j]); k++)
{ dr = dt.NewRow();
dr["PromoStartDate"] = sa[j].ToString();
dr["PromoEndDate"] = sb[j].ToString();
dr["ExecutionDate"] = sc[j].ToString();
dr["ExpiryDate"] = sd[j].ToString();
dr["CampaignIDRange"] = k.ToString();
//dr["CampaignIDRange"] = sf[j].ToString();
dt.Rows.Add(dr); }
noticeDetails.DataSource = dt.DefaultView;
noticeDetails.DataBind(); } }
protected void Button2_Click(object sender, EventArgs e) {
} } }
Basically it is the form when user enter all the data and press the submit button then the
data will appear on the grid view . On grid view there is option edit when i edit the row
and press the update button then the following error will occur
int cid = Int32.Parse(noticeDetails.DataKeys[e.RowIndex].Value.ToString());
"Argumentout of range exception and was unhandled by user code.
Index was out of range must be non negative and out of collection
Parameter Name: Index
please help me to resolve this error