0

i have this code in my page

 public partial class SiteMaster : MasterPage
    {
        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        private String truckID = "";
        private String userID = "";


        protected void Page_Load(object sender, EventArgs e)
        {         
            try
            {
                userID = Session["userid"].ToString();
                truckID = Session["truck_ID"].ToString();

                hyperlink_Profile.NavigateUrl = "~/View/Profile.aspx?u=" + userID;
                hyperlink_Inspection.NavigateUrl = "~/View/Inspection.aspx?u=" + truckID;
                hyperlink_MaintenanceChecklist.NavigateUrl = "~/view/maintenanceChecklist.aspx?u=" + truckID; 
                hyperlink_MaintenanceSchedule.NavigateUrl = "~/View/MaintenanceSchedule.aspx?u=" + truckID;
                hyperlink_MaintenanceRecord.NavigateUrl = "~/View/MaintenanceRecord.aspx?u=" + truckID;
                hyperlink_TruckData.NavigateUrl = "~/View/TruckData.aspx?u=" + truckID;
            }
            catch (HttpException ex)
            {
                System.Diagnostics.Debug.WriteLine("ERROR: " + ex.ToString());
            }

           MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
           MySqlCommand getRegistration = new MySqlCommand("Select registration_no from trucks where truck_id = " + truckID + "");
           MySqlDataReader setRegistration = getRegistration.ExecuteReader();
           RegistrationNo.Text = setRegistration.ToString();


        }
}

I am working with some ASP.Net webforms project and I am receiving an exception error in
MySqlDataReader setRegistration = getRegistration.ExecuteReader();

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

what could be the possible error of this?

1 Answer 1

2

There are several problems with your code:

  1. You don't open your connection.
  2. You don't assign a connection to the command. You can pass it as 2nd argument in the constructor.
  3. You call MySqlDataReader.ToString(), which will just return the type name. If you want to get only 1 value from the query just use ExecuteScalar().
  4. You don't close your connection. It implements IDisposable, you should put it in a using statement.
  5. You create your query by concatenating strings. It's a terrible practice and a huge security issue. Use parameters.
Sign up to request clarification or add additional context in comments.

1 Comment

Good analysys, but I have already explained the process to him, however the poster doesn't listen and falls from one error to next one. (Sooner or later its total lacks of acceptance on its previous questions will neglect him also these answers)

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.