错误:用户代码无法处理SqlException [英] Error:SqlException ubhandled by user code

查看:106
本文介绍了错误:用户代码无法处理SqlException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 31 col 9 in the code line{cnn.Open();} shows the Exception error:
SqlException was unhandled by user code

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)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

推荐答案

似乎您无法连接到sql server实例.更好地使用异常处理机制来解决此问题.
请检查...
1.验证SQL Server实例的名称
2.验证您尝试连接的SQL Server实例是否已启动并正在运行
3.确认用户名正确.
4.确认密码为corerct.

当您尝试打开连接时将发生异常.

问候
Sebastian
It seems that you are not able to connect to sql server instance. Better use exception handling mechanisms to tackle this problem.
Please check...
1. Verify the name of SQL server instance
2. Verify Whether the SQL server instance you are trying to connect is up & running
3. Verify the username is correct.
4. Verify the password is corerct.

Exception will be occuring when you are trying to open connection.

Regards
Sebastian


检查您的连接字符串,当您在连接字符串中提供错误值时出现此异常,例如:-错误的服务器名,错误的用户名,错误的密码
check your connection String, this exception comes when you are supplying wrong values in connectionstring like :- wrong servername,wrong username, wrong password


这篇关于错误:用户代码无法处理SqlException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆