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.  
            }
        }
 
Use 4.8 all the time

You'd need to tell us what some of the errors are

Error when I changed from 4.7.2 to 4.8
When I reverse it back to 4.7.2 the error remains.

////////////////////////////////////////////////////////////////////////////

Error CS0234 The type or namespace name 'AMS_2007DataSet' does not exist in the namespace 'Apartment_Management' (are you missing an assembly reference?) Line 32

Error CS0246 The type or namespace name 'AMS_2007DataSet' could not be found (are you missing a using directive or an assembly reference?) Line 106

////////////////////////////////////////////////////////////////////////////

Error when changed from 4.7.2 to 4.8:
namespace Apartment_Management
{
    partial class Unit_2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.amS_2007DataSet1 = new Apartment_Management.AMS_2007DataSet();
            this.dataGridView2 = new System.Windows.Forms.DataGridView();
            this.label_Property_Name = new System.Windows.Forms.Label();
            this.comboBox_Name_2 = new System.Windows.Forms.ComboBox();
            this.amS2007DataSet1BindingSource = new System.Windows.Forms.BindingSource(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.amS_2007DataSet1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.amS2007DataSet1BindingSource)).BeginInit();
            this.SuspendLayout();
            //
            // amS_2007DataSet1
            //
            this.amS_2007DataSet1.DataSetName = "AMS_2007DataSet";
            this.amS_2007DataSet1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
            //
            // dataGridView2
            //
            this.dataGridView2.AllowUserToOrderColumns = true;
            this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView2.Location = new System.Drawing.Point(12, 101);
            this.dataGridView2.Name = "dataGridView2";
            this.dataGridView2.RowHeadersWidth = 51;
            this.dataGridView2.RowTemplate.Height = 24;
            this.dataGridView2.Size = new System.Drawing.Size(983, 400);
            this.dataGridView2.TabIndex = 0;
            this.dataGridView2.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView2_CellContentClick);
            this.dataGridView2.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.DataGridView2_CellFormatting);
            //
            // label_Property_Name
            //
            this.label_Property_Name.AutoSize = true;
            this.label_Property_Name.Font = new System.Drawing.Font("Microsoft Sans Serif", 10.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label_Property_Name.Location = new System.Drawing.Point(392, 33);
            this.label_Property_Name.Name = "label_Property_Name";
            this.label_Property_Name.Size = new System.Drawing.Size(130, 22);
            this.label_Property_Name.TabIndex = 4;
            this.label_Property_Name.Text = "Property Name";
            //
            // comboBox_Name_2
            //
            this.comboBox_Name_2.FormattingEnabled = true;
            this.comboBox_Name_2.ImeMode = System.Windows.Forms.ImeMode.On;
            this.comboBox_Name_2.Location = new System.Drawing.Point(395, 58);
            this.comboBox_Name_2.Name = "comboBox_Name_2";
            this.comboBox_Name_2.Size = new System.Drawing.Size(208, 24);
            this.comboBox_Name_2.TabIndex = 5;
            this.comboBox_Name_2.SelectedIndexChanged += new System.EventHandler(this.comboBox_Name_2_SelectedIndexChanged);
            //
            // amS2007DataSet1BindingSource
            //
            this.amS2007DataSet1BindingSource.DataSource = this.amS_2007DataSet1;
            this.amS2007DataSet1BindingSource.Position = 0;
            //
            // Unit_2
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1007, 513);
            this.Controls.Add(this.comboBox_Name_2);
            this.Controls.Add(this.label_Property_Name);
            this.Controls.Add(this.dataGridView2);
            this.Name = "Unit_2";
            this.Text = "Unit_2";
            this.Load += new System.EventHandler(this.Unit_2_Load);
            ((System.ComponentModel.ISupportInitialize)(this.amS_2007DataSet1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.amS2007DataSet1BindingSource)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        #endregion

        private AMS_2007DataSet amS_2007DataSet1;
        private System.Windows.Forms.DataGridView dataGridView2;
        private System.Windows.Forms.Label label_Property_Name;
        private System.Windows.Forms.ComboBox comboBox_Name_2;
        private System.Windows.Forms.BindingSource amS2007DataSet1BindingSource;
    }
}
 
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
 
Back
Top Bottom