3

I have the following static method.

public static List<string> GetAutoCompleteData(string StudentId)
    {
            List<string> result = new List<string>();
            using (SqlConnection con = new SqlConnection("Data Source=.;Integrated Security=true;Initial Catalog=SMS"))
            {
                //using (SqlCommand cmd = new SqlCommand("select  StudentId,StudentName from tblStudent where StudentId LIKE '%'+@SearchText+'%'", con))
                using (SqlCommand cmd = new SqlCommand("select  T1.StudentName,T1.StudentId from tblStudent T1 where StudentId LIKE '%'+@SearchText+'%' except  select T2.StudentName, T2.StudentId from tblStudentAcademics T2 where T2.AcademicYear='" +Dropdownvalue + "'", con))
                {

                    con.Open();
                    cmd.Parameters.AddWithValue("@SearchText", StudentId);
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        result.Add(dr["StudentId"].ToString() + "-" + dr["StudentName"].ToString());
                    }
                    return result;
                }

        }
    }

I need to write ddlAcadeic.selectedvalue.tostring() in place of Dropdownvalue.Help me

Bikash Panigrahi
  • 41
  • 1
  • 3
  • 14

1 Answers1

2

Dropdownlist object is non static member of your class (Web Page) and static methods can not access not static members. Pass the dropdownList value to static method when you call it.

Static Members

Static methods and properties cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.

Static method definition

public static List GetAutoCompleteData(string StudentId, string dropdownvalue ) {
  //Your code
}

Static method call

StaticMethodClass.GetAutoCompleteData("studendId", dropdown.SelectedValue);
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Dropdownvalue should be ddlAcademic.selectedValue.tostring().Inside the static function ddlAcademics is not coming. – Bikash Panigrahi Jun 30 '14 at 07:21
  • Because static method could not access class members, you do not need ToString as SelectedValue is string property – Adil Jun 30 '14 at 07:24
  • I do not have a static class.I am using this static function for an ajax call.I need to get the dropdown selected value on text changing event of a textbox.I need to write in a sqlcommand to complete a query(for example select * from table where academic='"+dropdownvalue+"'.What should be the dropdown value? – Bikash Panigrahi Jun 30 '14 at 07:27
  • Check my updated answer, static members can not access non static members in they are in non-static class – Adil Jun 30 '14 at 07:31
  • So it is not possible to access a non static dropdownlist inside a static function? – Bikash Panigrahi Jun 30 '14 at 07:37
  • By passing as parameter – Adil Jun 30 '14 at 07:39
  • If I am passing a parameter inside a static function how I am going to access the value? – Bikash Panigrahi Jun 30 '14 at 07:45
  • public static List GetAutoCompleteData(string StudentId, DropDownList dropdown ) { string val = dropdown.SelectedValue; //Your code } – Adil Jun 30 '14 at 07:46
  • No need to write the Id of dropdownlist like ddlAcademic.selectedvalue.My problem is the id of the dropdownlist is not coming inside the static function. – Bikash Panigrahi Jun 30 '14 at 08:03
  • I got the answer.we can use the following. protected void ddlAcademic_SelectedIndexChanged(object sender, EventArgs e) { Session["Academic"] = ddlAcademic.SelectedValue.ToString().Trim(); } i have used this session in the static function as context.Session["Academic"].it worked. – Bikash Panigrahi Jun 30 '14 at 09:52
  • You could have changed the method to take the SelectedValue instead of using sessions – Adil Jun 30 '14 at 09:55
  • I tried for the same but the value did not come.So i tried this.Any issues am I gonna face if I use session? – Bikash Panigrahi Jun 30 '14 at 09:57
  • Using session here seems needles and using session intensively without need may degrade performance. As for as the passing value, you have to call that method GetAutoCompleteData from ddlAcademic_SelectedIndexChanged and pass the ddlAcademic.SelectedValue – Adil Jun 30 '14 at 10:01