1

I'm having an issue where I am getting an exception and I'm not sure how to handle it, as I thought I did.

On the reader = command.ExecuteReader() I'm getting the following exception message:

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

There isn't any data to pull through, which is fine, that's why I added the if(reader.HasRows) statement for in case there's no data on page load.

How should I be handling this?

Abbreviated code below:

protected void Page_Load(object sender, EventArgs e)
{
    using (SqlConnection connection = new SqlConnection(
        ConfigurationManager.ConnectionStrings["LocalConnection"].ConnectionString))
    {
        string selectSql = "SELECT tb1,tb2 FROM Stuff INNER JOIN User ON " + 
            "User.Stuff_FK=Stuff.ID";

        using (SqlCommand command = new SqlCommand(selectSql, connection))
        {
            // populate already existing data
            SqlDataReader reader;

            connection.Open();
            reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    TB1.Text = reader["tb1"].ToString();
                    TB2.Text = reader["tb2"].ToString();
                }
                reader.Close();
            }
            else
            {
                connection.Close();
            }
        }
    }
}
Sarath Subramanian
  • 20,027
  • 11
  • 82
  • 86
SelrekJohn
  • 476
  • 2
  • 6
  • 21

1 Answers1

4

User is a reserved keyword, you need to wrap it in []:

string selectSql = "SELECT tb1,tb2 FROM Stuff INNER JOIN [User] ON [User].Stuff_FK=Stuff.ID";

For that reason i would have chosen a different table-name. By the way, Stuff is also a T-SQL function, but that should work anyway if that table exists.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Yeah that was poor judgement, caught that just after posting this, however since changing that I'm getting "Additional information: Incorrect syntax near '='. Incorrect syntax near '='." Any ideas? – SelrekJohn Dec 09 '14 at 10:54
  • Stuff doesn't exist - it was for simplicity sake. The issue is still with the reader = command.Execute(); with the above error. – SelrekJohn Dec 09 '14 at 10:57
  • @user3626037 First try to run your sql query directly in the management studio. Once you get it corrected then run it from here. – Piyush Dec 09 '14 at 10:57
  • @user3626037: [Such unexpected problems can appear when you copy the code from a web page or email and the text contains unprintable characters like individual CR or LF and non-braking spaces](http://stackoverflow.com/questions/19730441/incorrect-syntax-near) – Tim Schmelter Dec 09 '14 at 10:58
  • @user3626037: so what is the real sql query that you are using if the above sql was "simplified"? – Tim Schmelter Dec 09 '14 at 11:00
  • It's just much longer, repeated variables in the While loop and more values in the selectSql String. I'm unsure of error as it's just saying 'near the '='' - I wouldn't have thought that would point to an SQL issue, more my C# syntax. – SelrekJohn Dec 09 '14 at 11:02