Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

When I execute my code below, this error message occurs:

"An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near '='. "

And this is the code:

string position;

SqlConnection con = new SqlConnection("server=free-pc\\FATMAH; Integrated Security=True; database=Workflow; ");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT EmpName FROM Employee WHERE EmpID=" + id.Text, con);

SqlDataReader Read = cmd.ExecuteReader();

if (Read.Read()==true)
{
    position = Read[0].ToString();
    Response.Write("User Registration successful");
}
else
{
    Console.WriteLine("No Employee found.");
}

Read.Close(); 

What is causing this, and how can I resolve it?

like image 965
F 505 Avatar asked Nov 18 '13 08:11

F 505


People also ask

What is System data SqlClient SqlException?

Data. SqlClient. SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing 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.

What is the class property in SqlException?

Property ValueA value from 1 to 25 that indicates the severity level of the error.


1 Answers

use try-catch to see real error occurred on you

 try
{
  //Your insert code here
}
catch (System.Data.SqlClient.SqlException sqlException)
{
  System.Windows.Forms.MessageBox.Show(sqlException.Message);
}
like image 103
Yuresh Karunanayake Avatar answered Sep 29 '22 20:09

Yuresh Karunanayake