Shesh Mishra

Shesh Mishra

  • NA
  • 22
  • 31.9k

Error:SqlException ubhandled by user code

Apr 26 2012 10:16 PM
While debugging the asp.net web page the error shows :SqlException was unhandled by user code.
the source code of the page is as below:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="executiveinfo.aspx.cs" Inherits="executiveinfo" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<!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>Untitled Page</title>
<style type="text/css">
.style1
{
width: 100%;
height: 343px;
}
.style2
{
}
.style3
{
}
.style4
{
width: 189px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<br />
<br />
<asp:Panel ID="Panel1" runat="server" Height="342px" Width="395px">
<table class="style1">
<tr>
<td class="style3" colspan="2">
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td class="style4">
Executive Name*</td>
<td>
<asp:TextBox ID="txtexecutivename" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
Branch Code*</td>
<td>
<asp:DropDownList ID="ddlbranchcode" runat="server" Width="146px">
<asp:ListItem>01</asp:ListItem>
<asp:ListItem>02</asp:ListItem>
<asp:ListItem>03</asp:ListItem>
<asp:ListItem>04</asp:ListItem>
<asp:ListItem>05</asp:ListItem>
<asp:ListItem>06</asp:ListItem>
<asp:ListItem>07</asp:ListItem>
<asp:ListItem>08</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style4">
Address*</td>
<td>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
City*</td>
<td>
<asp:DropDownList ID="ddlcity" runat="server" AutoPostBack="True">
<asp:ListItem>Kanpur</asp:ListItem>
<asp:ListItem>Lucknow</asp:ListItem>
<asp:ListItem>Muradabad</asp:ListItem>
<asp:ListItem>Bareilly</asp:ListItem>
<asp:ListItem>Hardoi</asp:ListItem>
<asp:ListItem>Unnao</asp:ListItem>
<asp:ListItem>Shahjahanpur</asp:ListItem>
<asp:ListItem>Rai Bareilly</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="style4">
Email Id*</td>
<td>
<asp:TextBox ID="txtemailid" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
Gender*</td>
<td>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr>
<td class="style4">
Mobile no*</td>
<td>
<asp:TextBox ID="txtmobileno" runat="server"></asp:TextBox>
<cc1:FilteredTextBoxExtender ID="txtmobileno_FilteredTextBoxExtender"
runat="server" Enabled="True" FilterType="Numbers"
TargetControlID="txtmobileno">
</cc1:FilteredTextBoxExtender>
</td>
</tr>
<tr>
<td class="style4">
Remark</td>
<td>
<asp:TextBox ID="txtremark" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style4">
</td>
<td class="style2" colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit"
Width="135px" />
</td>
</tr>
</table>
</asp:Panel>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</ContentTemplate>
</asp:UpdatePanel>

</div>
</form>
</body>
</html>

and the C# code is:


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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;

public partial class executiveinfo : System.Web.UI.Page
{
SqlConnection cnn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=LMS;Data Source=SHESH-PC\SHESHSQLEXPRESSS");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

SqlCommand cm = new SqlCommand();
string sql = "insert into executiveinfo(executivename,branchcode,address,city,emailid,gender,mobileno,remark,entrydate)values('" + txtexecutivename.Text + "','" + ddlbranchcode.SelectedItem.ToString() + "','" + txtaddress.Text + "','" + ddlcity.SelectedItem.ToString() + "','"+txtemailid.Text+"','" +RadioButtonList1.SelectedItem.ToString() + "'," + txtmobileno.Text + ",'" + txtremark.Text + "','" + DateTime.Now.ToString() + "')";
cm.Connection = cnn;
cm.CommandType = CommandType.Text;
cm.CommandText = sql;
int a = 0;
cnn.Open();
a = cm.ExecuteNonQuery();
cnn.Close();
if (a == 0)
{
lblmsg.Text = "DATA NOT SAVED";
}
else
{

lblmsg.Text = "DATA SAVED";

}


}
}


and the error is at line 48 col 1 in the code line{cnn.Open();} and the table is dbo.executiveinfo

and schema of table is:

Column_name Type Computed Length Prec Scale
id numeric no 9 18 0
executivename varchar no 50
branchcode varchar no 30
address varchar no 50
city varchar no 50
emailid varchar no 50
gender varchar no 50
mobileno numeric no 9 18 0
remark varchar no 50
entrydate datetime no 8
activestatus bit no 1


Answers (5)