selvi subramanian

selvi subramanian

  • NA
  • 818
  • 421.9k

ExecuteNonQuery requires an open and available Connection

May 19 2014 4:57 AM
my design
<%@ Page Language="C#" MasterPageFile="~/MasterPage/Employe.master" AutoEventWireup="true" CodeFile="Total Days.aspx.cs" Inherits="Employee_Total_Days" Title="Worked Days" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content8" runat="server" contentplaceholderid="Header_Menu">

<div id="example">
<ul id="na">
<li class="current"><a href="../Admin/Login.aspx">Registration</a></li>
<li><a href="#">Report</a>
<ul>
<li><a href="../Admin/Day Sales.aspx">Sales</a></li>
<li><a href="../Admin/SectionWiseSales.aspx">Purchas</a></li>
<li><a href="#">Purchase</a></li>
</ul>
</li>
<li><a href="#">Help</a>
</li>

</ul>
<br />
<br />
<br />
<br />
</div>

</asp:Content>
<asp:Content ID="Content9" runat="server" contentplaceholderid="HeaderBar">
<div id="BrownHeaderContent">
<div id="BrownHeader">
<br />
</div>


</div>

</asp:Content>
<asp:Content ID="Content10" runat="server" 
contentplaceholderid="ContentPlaceHolder1">
<p>
<br />
</p>

</asp:Content>
<asp:Content ID="Content11" runat="server" contentplaceholderid="ContentReport">
<form id="form1" runat="server">

<asp:MultiView ID="MultiView1" runat="server">

<asp:View ID="View1" runat="server">
<table visible="False">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="ID"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="TextBox2" runat="server" Visible="False"></asp:TextBox>
</td>
</tr>
<tr align="center">
<td align="center" colspan="2"> 
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/show.jpg" 
Width="89px" onclick="ImageButton1_Click" />
&nbsp;&nbsp;
</td>
</tr>
</table> 
</asp:View> 
<asp:View ID="View2" runat="server">
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
GridLines="None" 
AutoGenerateColumns="False">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="Id" />
<asp:BoundField DataField="Date" HeaderText="Date" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="Section" HeaderText="Section" />
<asp:BoundField DataField="Intime" HeaderText="Intime" />
</Columns> 
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Total presents
<asp:Label ID="lblStatusCount" runat="server" />

<br />

<asp:Button ID="Button1" runat="server" Text="Clear" onclick="Button1_Click" 
Width="83px" Font-Bold="True" ForeColor="Black" />

</asp:View>
</asp:MultiView>


</form>
</asp:Content>
<asp:Content ID="Content12" runat="server" 
contentplaceholderid="FullContentArea">
<p>
<br />
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>

</asp:Content>
<asp:Content ID="Content13" runat="server" contentplaceholderid="Footer">
Copyrights @ MarvelBeeShop 2014
</asp:Content>




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


Answers (4)