0

Hi, i am trying to pass the value of a text box to a sql statement, the only thing is that the sql statement is located in a different file to where the text boxes are. i have had some trouble in the past with this, as when i call the value in my sql statement i get an error stating that " an object reference is required for the non static field, mehtod or property", i have changed the modifiers of my text box to pubic and this still has not worked, our there another other soultions?

This is were i get my values that i want to put into the sql statement

txtfrom.Text = dtResults.Rows[0]["asset_open_per"].ToString(); // Date from - Default to Database value 
this.txtto.Text = DateTime.Today.ToString("yyyyMM");  // Date To - Current Year and Month   
      {

dtResults = SQLMETHODS.GetPostings2(strAssetNumber, PeriodFromV, PeriodToV);

      }

this is my sql statement and i get the error from the "periodFrom" & "periodTo"

 public static DataTable GetPostings2(string AssetNumberV, string PeriodFromV, string PeriodToV)
      public static DataTable GetPostings2(string AssetNumberV, string PeriodFromV, string PeriodToV)
        {
            DataTable dtGetPostings2;
            try
            {
                dtGetPostings2 = new DataTable("GetPostings");

                SqlParameter AssetNumber = new SqlParameter("@AssetNumber", SqlDbType.VarChar, 6);
                AssetNumber.Value = AssetNumberV;

                SqlParameter PeriodFrom = new SqlParameter("@PeriodFrom", SqlDbType.VarChar, 6);
                PeriodFrom.Value = frmAssetPostings.txtfrom.Text; 


                SqlParameter PeriodTo = new SqlParameter("@PeriodTo", SqlDbType.VarChar, 6);
                PeriodTo.Value = frmAssetPostings.txtto.Text; 

                SqlCommand scGetPostings2 = new SqlCommand("SELECT * FROM [POSTING] WHERE [ASSET_NO] = @AssetNumber And PERIOD >= @PeriodFrom AND PERIOD <= @PeriodTo ORDER by PERIOD, JOUR_REF, JOUR_LINE", DataAccess.AssetConnection);


                SqlDataAdapter sdaGetPostings2 = new SqlDataAdapter();
                sdaGetPostings2.SelectCommand = scGetPostings2;
                sdaGetPostings2.Fill(dtGetPostings2);

                return dtGetPostings2;

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Retriving Posting Details: Processed with this error:" + ex.Message);
                return null;
            }
        }   {
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by gusano79
0

i get an error stating that " an object reference is required for the non static field, mehtod or property", i have changed the modifiers of my text box to pubic and this still has not worked

Assuming the error is referring to the text box lines, it isn't about access modifiers. What it's trying to tell you is that GetPostings2 doesn't know anything about the object you know as frmAssetPostings.

You could "solve" this by defining GetPostings2 as part of a class that has a reference to the form, or even by passing the form as a parameter to the method--but this isn't a good design pattern.

Really, your GetPostings2 method doesn't really need to know anything about the forms in the application that calls it, just the values of the "from" and "to" text boxes. Making the entire form available is overkill, and makes this method only work with the specific form you've designed.

Instead, just make the "from" and "to" values additional string parameters to the method. If you do that, the method can be called from anywhere that can provide values for the parameters--this is a Good Thing(tm) and is a variety of loose coupling.

Edited by gusano79: clarity

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.