Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there, I have a problem where visual studio throws no error but the code does. When the try and catch statements are inplace they throw back the error and the program stops. However when I take out the try and catch the program loads etc but the code does do as I am expecting. I may be looking at this in a totally unorthadox way and thats the problem but to me it looks like it should work.

Below is the sql code section and also how I am calling it.

public void getUser()
       {
           SqlConnection conn = new SqlConnection(Calorie_Counter_UK.Properties.Settings.Default.Calorie_CounterConnectionString);
           conn.Open();
         try
          {
               using (SqlCommand command = new SqlCommand("SELECT * FROM [User] LIMIT 1",conn))
               {
                   SqlDataReader reader = command.ExecuteReader();
                   while (reader.Read())
                   {
                       name = reader.GetString(0);
                       age = reader.GetInt16(1);
                       height = reader.GetInt16(2);
                       weight = reader.GetDecimal(3);
                       target = reader.GetDecimal(4);
                   }
               }
         }
         catch
        {
             MessageBox.Show("Problem with update");
        }
       }
       public string getName()
       {
           return name;
       }
       public int getAge()
       {
           return age;
       }
       public int getHeight()
       {
           return height;
       }
       public decimal getWeight()
       {
           return weight;
       }
       public decimal getTarget()
       {
           return target;
       }


private void Window_Loaded(object sender, RoutedEventArgs e)
       {
           User_database user = new User_database();
           user.getUser();
           txtName.Text = user.getName();
           nudAge.Value = user.getAge();
           nudHeight.Value = user.getHeight();
           nudWeight.Value = Convert.ToDouble(user.getWeight());
           nudTargetWeight.Value = Convert.ToDouble(user.getTarget());
       }


It is frustrating me a little bit because I can't get an error message to even help me along! If someone could shed some light it would be appreciated!

Dan
Posted
Comments
walterhevedeich 1-Aug-11 7:39am    
Sounds weird. But have you tried catching the actual exception and then putting it in your messagebox? It might be helpful if we can see the actual error.
Herman<T>.Instance 1-Aug-11 7:40am    
you use the SqlConection object which is for MS Sql Server and you use a MySQL query (LIMIT 1 should be TOP 1 in T-SQL )
[no name] 1-Aug-11 7:42am    
Nice catch!!!
Herman<T>.Instance 1-Aug-11 7:47am    
thank you
[no name] 1-Aug-11 7:42am    
Either log/display the exception in the catch block or remove the try/catch altogether and see what exception it throws.

Why aren't you putting the SqlConnection stuff inside the try block?

I would step through it with the debugger to see what's happening.
 
Share this answer
 
Comments
DanHodgson88 1-Aug-11 9:11am    
John, To be honest with you im pretty new at this so using try and catch blocks doesn't come naturally to me yet so me putting it in the wrong place doesn't surprise me! I will alter the code to ensure the sqlconnection and other bits nad bobs are within the try catch :)
This code should tell you what the problem might be:
C#
public void getUser()
{
	SqlConnection conn = new SqlConnection(Calorie_Counter_UK.Properties.Settings.Default.Calorie_CounterConnectionString);
	using (SqlCommand command = new SqlCommand("SELECT TOP 1 * FROM [User] ",conn))
	{
		try
		{
			command.Connection.Open();
			SqlDataReader reader = command.ExecuteReader();
			while (reader.Read())
			{
				name = reader.GetString(0);
				age = reader.GetInt16(1);
				height = reader.GetInt16(2);
				weight = reader.GetDecimal(3);
				target = reader.GetDecimal(4);
			}
		}
		catch (Exception err)
		{
			MessageBox.Show(err.ToString()); // provide that message in 
		}
		finally
		{
			if (command.Connection.State.Equals(ConnectionState.Open))
			{
				command.Connection.Close();
				command.Connection.Dispose();
			}
		}
	}
}
 
Share this answer
 
Comments
DanHodgson88 1-Aug-11 9:06am    
spot on mate cheers :)
You have not mentioned what database it is. If it is SQL Server (as you have used SqlConnection class) then, as digimanus suggested, rewrite your query as
SQL
SELECT TOP 1 * FROM [User]

There is no LIMIT clause in MS SQL Server.

If you database is indeed MySQL, then use the appropriate Library to access the database, you cannot use SqlConnection in that case.

And your try/catch code is horrible. If at all you use try/catch, use the catch block to log the actual exception message.
 
Share this answer
 
Comments
DanHodgson88 1-Aug-11 9:06am    
Thanks :) appreicate your time :)
[no name] 2-Aug-11 10:24am    
you're welcome :-)
This probably happens due to LIMIT key word. If you are using SQL Server, it might not support the key word. Take a look at this Channel 9 discussion. SQL Server and LIMIT clauses on SELECT statements[^]
 
Share this answer
 
Comments
DanHodgson88 1-Aug-11 9:06am    
spot on thanks :)
Wonde Tadesse 3-Aug-11 21:26pm    
Thanks.
It might be helpful,

sql_top[^]

I tried with following code and it works,

C#
public static void getUser()
       {
           SqlConnection conn = new SqlConnection(@"YOU CONNECTION STRING");
           conn.Open();
           try
           {
               using (SqlCommand command = new SqlCommand("SELECT Top 1 * FROM [YOURTABLENAME]", conn))
               {
                   SqlDataReader reader = command.ExecuteReader();
                   while (reader.Read())
                   {
                       var name = reader.GetString(0);
                       var age = reader.GetInt16(1);
                       var height = reader.GetInt16(2);
                       var weight = reader.GetDecimal(3);
                       var target = reader.GetDecimal(4);
                   }
               }
           }
           catch
           {
               Console.WriteLine("Problem with update");
           }
       }
   }


:)
 
Share this answer
 
v2
Comments
DanHodgson88 1-Aug-11 9:05am    
Thank you very much for testing that :) it was me being niave searching for how to chose one row in sql and not realising that depending on the server it differs. But again thanks for your help :)
Mohammad A Rahman 1-Aug-11 9:07am    
Not a problem :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900