1

I have a question according to this code. VisualStudio shows no errors or warnings but when I run it, the result is only the exceptionerror ("Something went wrong."). This is how I have always done it before but somehow always worked except for now. Am I missing a simple thing?

        protected void Page_Load(object sender, EventArgs e)
    {
        // Connect
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\royva\documents\visual studio 2013\Projects\CookieMultiView\CookieMultiView\App_Data\Databank.mdb';Persist Security Info=True";
        // Execute
        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
        cmd.CommandText = "SELECT * FROM teachers = ?";// + Request.QueryString["id"];
        lbl.Text = "";

        cmd.Parameters.AddWithValue("id",Request.QueryString["id"]);


        // Read
        try
        {
           conn.Open();
           OleDbDataReader reader = cmd.ExecuteReader();
           while (reader.Read())
           {
               lbl.Text = reader["teacherid"].ToString();
           }
        }
        catch (Exception ex)
        {
            //lbl.Text = ex.StackTrace;                
            lbl.Text = "Something went wrong.";
        }
        finally
        {
            conn.Close();
        }
4
  • 1
    Remove lbl.Text = "Something went wrong."; and then put a break point on lbl.Text = ex.StackTrace; and step through it. Also you're adding a param id but it's not in your select query Commented Jun 2, 2016 at 8:11
  • 2
    Check your commandText i think it should be something like that: cmd.CommandText = "SELECT * FROM teachers WHERE id = ?" Commented Jun 2, 2016 at 8:12
  • 1
    Put a breakpoint at lbl.Text = "Something went wrong."; line and see what is the actual exception. As of now, it can be any of the hundred issues. Commented Jun 2, 2016 at 8:15
  • I've changed the commandtext and its sadly still not fixed. Also i've used the breakpoint and got 3 exceptions, access to patch 'c/blabla/temporary ASP.NET Files' is denied. "Invalid file name for file monitoring" "no value given for one or more required parameters" Commented Jun 2, 2016 at 8:50

1 Answer 1

1

For Detailed info of the Exception,

catch (Exception ex)
 {
   //Either you can write log or display in label
   lbl.Text = ex.Message;
 }

Also check the folder access rights for

Data Source='C:\Users\royva\documents\visual studio 2013\Projects\CookieMultiView\CookieMultiView\App_Data\Databank.mdb'

Programmatically to check for specific files use File.Exists(path), which will return a boolean indicating whether the file at path exists.

And validate if the connection has been established or not.

Sign up to request clarification or add additional context in comments.

3 Comments

It's a local database and the database is put there via visual studio's. The error i get with your tip of ex.message is "No value given for one or more required parameters." This will help me out! Thanks :)
It was indeed the connection, somehow the database was on read-only. Got it working now! Cheers!
Good to hear @RoyvanderPlas :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.