I am writing a client application using System.Data.OracleClient. With an error in writing a select query when calling the OracleDataAdapter.Fill (DataSet) function, I understand that an error occurs inside, but the program, when executing this function, interrupts the execution of the body of the function in which this string is located. Binding OracleDataAdapter.Fill Error event or "try" construct does not fire.
How can I catch errors like this?
string select = "SELECT qwe FROM employee"; // 'employee' does not contain 'qwe'
OracleDataAdapter adapter = new OracleDataAdapter(select, connection);
adapter.FillError += Adapter_FillError;
try
{
adapter.Fill(ds, "zak");
}
catch (OracleException ex)
{
MessageBox.Show(ex.Message);
}
private void Adapter_FillError(object sender, FillErrorEventArgs e)
{
MessageBox.Show(e.Errors.Message);
}
Related
I'm trying to make a program with a login system
I'm new to this but I have been working 8 hours straight trying to fix this.
This is the error code I get
+ ServerVersion 'con.ServerVersion' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}
Here's my code
private void LogB_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=Myip;user id=MyId;database=MyDb;password=MyPw;persistsecurityinfo=True");
SqlDataAdapter sda = new SqlDataAdapter("Select * From login where navn='"+ TULog.Text + "' and pw='" + TPLog.Text + "'",con);
try
{
con.Open();
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
throw ex;
}
finally
{
con.Close();
}
}
}
}
Sorry If this seems like some crap but I'm a guy who's trying to learn :p
This exception is telling you that there was an attempt to access the con.ServerVersion property while the SqlConnection was closed.
From MSDN on the SqlConnection.ServerVersion property:
InvalidOperationException - The connection is closed. ServerVersion was called while the returned Task was not completed and the connection was not opened after a call to OpenAsync.
The code you show above does not show a call to this property, you must be doing so somewhere else. Regardless, the connection needs to be open prior to doing so.
I have a (Windows Forms) app that will be installed on various users' desktops; it will allow them to generate reports based on custom code that connects to a SQL Server Database and reads records from certain tables.
The Connection String is:
Data Source=PLATYPUS;Initial Catalog=Duckbills;Persist Security Info=True;User ID=lilabner;Password=d0GpAtCh42;Connect Timeout=120
I understand this to mean that if all the following is true:
The user's machine has the SQL Server client software installed
The SQL Server client has been configured to access the PLATYPUS database
The table "Duckbills" exists in that database
The username and password are what is expected
...then the connection will be successful.
In the event any of the above equate to false, I want to show the user a "user-friendly" message informing them, in plain English, what the problem is and what to do about it. How can I test for these various problems so that the most appropriate message is shown the user in the event of connection failure.
Here is the pertinent existing code:
DataSet dsUsage = new DataSet();
SqlConnection conn =
new SqlConnection("SERVER=PLATYPUS;DATABASE=Duckbills;UID=lilabner;PWD=d0GpAtCh42;Connection Timeout=0");
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Exec sp_ViewPlatypi";
da.SelectCommand = cmd;
conn.Open();
da.Fill(dsUsage);
conn.Close();
DataTable dtUsage = dsUsage.Tables[0];
if (dtUsage.Rows.Count > 0)
{
foreach (DataRow productUsageByMonthDataRow in dtUsage.Rows)
{
. . .
catch (Exception ex)
{
String exDetail = String.Format(PlatypusConstsAndUtils.ExceptionFormatString, ex.Message, Environment.NewLine, ex.Source, ex.StackTrace);
MessageBox.Show(exDetail);
}
As you can see, I have a "catch all" (no pun intended) Catch block. I want something like:
catch (SQLServerException sex)
{
MessageBox.Show("SQL Server not available - go tell the DBA");
}
catch (NoTableException ntex)
{
MessageBox.Show("Go tell the DBA there's no such table");
}
catch (BadPwdException sex)
{
MessageBox.Show("Your username and/or password are bad - go tell it to the Marines");
}
catch (Exception ex)
{
String exDetail = String.Format(PlatypusConstsAndUtils.ExceptionFormatString, ex.Message, Environment.NewLine, ex.Source, ex.StackTrace);
MessageBox.Show(exDetail);
}
...but I don't know, first if all, if it's even possible to get that granular with connection exception messages, and secondly - if it is - just what the corresponding Exception types are.
Strip your code back to handle Exception (catch (Exception ex)). Then, put a break point in your catch block. Attach the debugger to your code and when it hits the catch block, drag the ex variable in to your watch window. There, you will see all the details of the exception and you can determine what you need to be able to better handle the various exceptions that come up.
Based on MethodMan's suggestion and TheShaman's link, I adapted that code to this:
catch (SqlException sex)
{
for (int i = 0; i < sex.Errors.Count; i++)
{
String sexDetail = String.Format("SQL Exception #{0}{1}Source: {2}{1}Number: {3}{1}State: {4}{1}Class: {5}{1}Server: {6}{1}Message: {7}{1}Procedure: {8}{1}LineNumber: {9}",
i+1, // Users would get the fantods if they saw #0
Environment.NewLine,
sex.Errors[i].Source,
sex.Errors[i].Number,
sex.Errors[i].State,
sex.Errors[i].Class,
sex.Errors[i].Server,
sex.Errors[i].Message,
sex.Errors[i].Procedure,
sex.Errors[i].LineNumber);
MessageBox.Show(sexDetail);
}
}
catch (Exception ex)
{
String exDetail = String.Format(UsageRptConstsAndUtils.ExceptionFormatString, ex.Message, Environment.NewLine, ex.Source, ex.StackTrace);
MessageBox.Show(exDetail);
}
And for an example of what this produces:
I'd like my code to give a warning or something whenever the database is not available.
(That's why I commented the connection string), but my website just crashes.
I created this class cause there were many parts of the program using this connection string (login page, register page, user panel page, etc), so if I want to change something in the future, this is the only string I have to change.
namespace Fatture.Data.User
{
public class DbConnection
{
public MySqlConnection ConnessioneDb()
{
MySqlConnection conn = new MySqlConnection(/*ConfigurationManager.ConnectionStrings["ConnectionStringloginDb"].ConnectionString*/);
try
{
conn.Open();
}
catch (Exception ex)
{
throw new Exception("I can't connect to the database", ex);
}
return conn;
}
}
}
Use exception handling...
var connector = new DbConnection();
try
{
var connection = connector.ConnessioneDb();
}
catch (Exception ex)
{
// show message to user: database not available
}
Though you'll have to put this in place everywhere you access the database. Instead you can also just set up error handling for your entire site.
Catch the exception...
try
{
var conn = new DbConnection().ConnessioneDb();
}
catch (Exception ex)
{
// Notify user here
}
public partial class Variables_input : System.Web.UI.Page
{
public string conStr = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
protected void Buttonsubmit_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(conStr))
{
try
{
con.Open();
Response.Write("<script language ='javascript'>alert('Connection is open');</script>");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Response.Write("<script language ='javascript'>alert('Connection Problem');</script>");
}
finally
{
con.Close();
Response.Write("<script language ='javascript'>alert('Connection is close');</script>");
}
}
}
I am not able to connect to connect my SQL Server database. After running this code I receive "Connection Problem". Please help.
I don't know where to begin to start.
You are using SQL server not mySQL right, you have funny wording. Just want to make sure.
You might want to try looking at the exception data, usually it gives you an error message. You caught the error now read it.
If the error message is not helpful you could put a breakpoint at con.Open() and step into an make sure all the parameters are correct or see where the problem is if the exception error doesn't help.
Might also want to look at this:
dot net pearl for sqlconnection
I have a Windows services running in .NET 4.5. Everything works fine. However, when my service encounters a SqlException, it hangs (turns into a zombie).
I have a timer (System.Timers) that calls process. In process, locate cmd.ExecuteReader(). If I remove EXECUTE permissions from the stored procedure, I receive a SqlException (as expected). When this happens, the service simply hangs.
I would have expected one of the try {} catch blocks to capture the exception and exit the method gracefully. However, the system appears to hang on this call. I had a number of Trace statements in the code. I removed them so it would be easier to read.
private void TimerForNotification_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
TimerForNotification.Stop();
int count = new GetSMSNotifications().process();
TimerForNotification.Start();
}
public int process()
{
int count = 0;
// Get the ConnectionStrings collection.
ConnectionStringSettings connections = ConfigurationManager.ConnectionStrings["DE_OLTP"];
try
{
using (SqlConnection conn = new SqlConnection(connections.ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("[dbo].[get_SMSToSend]", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
try
{
SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
while (dr.Read())
{
// increment counter
count++;
string destinationAddress = Convert.ToString(dr[dr.GetOrdinal("DestinationAddress")]);
string alertMessage = Convert.ToString(dr[dr.GetOrdinal("Content")]);
// Send out the notification
sendPush(destinationAddress, alertMessage);
}
dr.Close();
}
catch (SqlException se)
{
DELog.Log.Error(se);
}
}
}
catch (Exception ex)
{
DELog.Log.Error(ex);
}
return count;
}
Interestingly, I created a console app that calls the above method and the try {} catch block works as expected.
I don't see any unhandled exceptions in the event log.
Thoughts?
Problem solved. The reason why it appeared my service was hanging is because I was a missing a reference to Entity.Framework.dll. When the service ran into an exception, the EF dll could not be found. I use the EF in my logging layer.
I was able to discover this issue by installing my service and then ATTACHing to the run process.
Try these things:
Add trace line to these:
TimerForNotification.Stop();
try
{
int count = new GetSMSNotifications().process();
}
catch (Exception ex)
{
// trace line here with ex.ToString().
}
TimerForNotification.Start();
// trace line here stating, i started at DateTime.Now;
Remove execute permissions and see if the above code generates a trace line.
If it does, then the exception details will show up. (even though your method looks very safe) Fix it accordingly.
If the catch doesn't trace, then you should see trace lines for the post start trace which means the service is working as expected.