<%@ Page Language="C#" MasterPageFile="~/MasterPage/Employe.master" AutoEventWireup="true" CodeFile="Total Days.aspx.cs" Inherits="Employee_Total_Days" Title="Worked Days" %>
Copyrights @ MarvelBeeShop 2014
my code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Employee_Total_Days : System.Web.UI.Page
{
Marvel mml = new Marvel();
DataSet sds = new DataSet();
SqlConnection con;
SqlDataAdapter sad = new SqlDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MultiView1.Visible = true;
MultiView1.SetActiveView(View1);
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
TextBox2.Text = DateTime.DaysInMonth(year, month).ToString();
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
MultiView1.SetActiveView(View2);
string vew = "select Id,Date,Name,Status,Section,Intime from aten where Id='" + TextBox1.Text + "'";
sds = mml.GETDS(vew);
GridView1.DataSource = sds;
GridView1.DataBind();
int count = 0;
foreach (GridViewRow row in this.GridView1.Rows)
{
if (row.Cells[3].Text == "Present")
{
count++;
}
}
this.lblStatusCount.Text = count.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=SELVI-PC\\SQLEXPRESS;Initial Catalog=bramandam;Persist Security Info=True;Integrated Security=True");
foreach (GridViewRow g1 in GridView1.Rows)
{
string sal = "insert into presentdays(Id,Date,Name,Status,Section,totaldays,present) values('" + g1.Cells[0].Text;
sal += "','" + g1.Cells[1].Text;
sal += "','" + g1.Cells[2].Text;
sal += "','" + g1.Cells[3].Text;
sal += "','" + g1.Cells[4].Text;
sal += "','" + TextBox2.Text;
sal += "','" + lblStatusCount.Text;
sal += "')";
mml.Insert(sal);
}
MultiView1.Visible = true;
MultiView1.SetActiveView(View1);
TextBox1.Text = "";
}
}
my error
Server Error in '/Birmandam' Application.
ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
Source Error:
| Line 57: catch (Exception e) Line 58: { Line 59: throw e; Line 60: } Line 61: finally |
Source File: d:\sample tools\Birmandam\App_Code\Marvel.cs Line: 59
Stack Trace:
| [InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.] Marvel.Insert(String qry) in d:\sample tools\Birmandam\App_Code\Marvel.cs:59 Employee_Total_Days.Button1_Click(Object sender, EventArgs e) in d:\sample tools\Birmandam\Employee\Total Days.aspx.cs:66 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 |

Abhay ShankerPosted May 19, 2014, 7:39 AM
if (con.State == ConnectionState.Closed)
con.Open();
Com.ExecuteNonQuery();
Also, remember invoke con.Close() method to close the connection if you don't plan to reuse the connection.
selvi subramanianPosted May 19, 2014, 6:39 AM
Jayesh DhamsaniyaPosted May 19, 2014, 5:08 AM
So We need to check that file code I think your connection string is wrong or you are forget con.open()
First Add
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=SELVI-PC\\SQLEXPRESS;Initial Catalog=bramandam;Persist Security Info=True;Integrated Security=True");
con.open();
......
}
try with this
Lakshmanan Sethu SankaranarayanPosted May 19, 2014, 5:06 AM