Procedure did not return an expected value

Lin100

Well-known member
Joined
Dec 12, 2022
Messages
69
Programming Experience
10+
After the procedure Obtain_Property_ID has ran, the Prop_ID suppose to be some value from 1 to 5, but instead it remains a 0.

string Prop_ID = "0";
Obtain_Property_ID(Prop_Name, Prop_ID);
R1.Reservation_Number.Text = "R-" + Prop_ID; <<--- Suppose to be a 3, but it has a value of 0.

C#:
       private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           string Apartment_Status;
           DateTime Date_Reserved = DateTime.Today;
           MessageBox.Show("Date_Reserved =  " + Date_Reserved);

           object value = this.dataGridView2.CurrentRow.Cells[7].Value.ToString();
           Apartment_Status = value.ToString();
           MessageBox.Show("Apartment_Status =  " + Apartment_Status);

           switch (Apartment_Status)
             {
                case "Reserved":
                  Reservation2 R2 = new Reservation2();
                  R2.Show();
                break;

                case "Vacant":
                  Reservation1 R1 = new Reservation1();
                  R1.Property_Name.Text = this.dataGridView2.CurrentRow.Cells[0].Value.ToString();

                  string Prop_Name = R1.Property_Name.Text;
                  string Prop_ID = "0";
                  Obtain_Property_ID(Prop_Name, Prop_ID);

                  R1.Reservation_Number.Text = "R-" + Prop_ID;
                  R1.Unit_Number.Text = this.dataGridView2.CurrentRow.Cells[1].Value.ToString();
                  R1.Date_Reserved.Text = Date_Reserved.ToString();
                  R1.Lease_Months.Text = "6";
                  R1.Reservation_Cancelled.Text = "No";
                  R1.Show();
                break;

                default:
                break;
             }

            void Obtain_Property_ID(string Prop_Name, string Prop_ID)
            {
               OleDbConnection con = new OleDbConnection();
               con.ConnectionString = ConfigurationManager.ConnectionStrings["Apartment_Management.Properties.Settings.AMS_2007ConnectionString"].ToString();
               con.Open();
               MessageBox.Show("Obtain_Property_ID procedure. Prop_Name =  " + Prop_Name);
               OleDbCommand cmd = new OleDbCommand();
           
               cmd.CommandText = "SELECT Property_ID FROM Property WHERE Property_Name = '" + Prop_Name + "' ";

               cmd.Connection = con;
               OleDbDataAdapter da = new OleDbDataAdapter(cmd);
               DataSet ds = new DataSet();
               da.Fill(ds);

               Prop_ID = (string)ds.Tables[0].Rows[0].ItemArray[0];
               MessageBox.Show("Property_ID = " + Prop_ID);   //This showed a 3, and it is correct
               //The MessageBox did showed a correct number of 3, but
               //after it exit the procedure, the variable Prop_ID no longer has value of 3
               // but a 0.  
            }
        }
 
Can you zip up the entire project (delete the "bin" and "obj" folders before you zip) and attach it here? It's going to be easier to have all the code to troubleshoot that one
 
Also if you're using strongly typed datasets I can show you some stuff to make the database side of things easier - visual studio will write most of the code in this thread for you
 
Parameters in C# are by-value by default. So the easiest way to think of this is that all parameters are local variables. So any changes you make within the method like changing what string is referenced by Prop_ID on line 53 will only have a local effect. If you want parameters to be passed by reference, use the ref modifier on them.

 

Latest posts

Back
Top Bottom