Click here to Skip to main content
15,894,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 textbox's first one called txtCountry and the second one called txtRegion work with AutoCompleteExtender to fetching data from database when you type any country or any region so the problem is i want to fetch only the region when the text box country = country in the database but the method that i use it doesn't take the txtCountry.text in the parameter and also i got error i will put it .

In database:
there are 2 tables called , Country and Region

and Country table related with Region as primary key table

This is page code:

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="Style/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods = "true">
</asp:ScriptManager>
 
        <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchCountry"
    MinimumPrefixLength="2"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtCountry"
    ID="AutoCompleteExtender1"  runat="server" FirstRowSelected = "false"   CompletionListCssClass="autocomplete_completionListElement"
  CompletionListItemCssClass="autocomplete_listItem"
  CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
</cc1:AutoCompleteExtender>
<br />
<br />
        <asp:TextBox ID="txtRegion" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="SearchRegion"
    MinimumPrefixLength="2"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtRegion"
    ID="AutoCompleteExtender2"  runat="server" FirstRowSelected = "false"   CompletionListCssClass="autocomplete_completionListElement"
  CompletionListItemCssClass="autocomplete_listItem"
  CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem">
</cc1:AutoCompleteExtender>
    </div>
    </form>
</body>
</html>


And this is the C# code with 2 methods called SearchCountry and SearchRegion:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> SearchCountry(string prefixText, int count)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager
                        .ConnectionStrings["Jobs_DBConnectionString"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select * from Country where " +
                    "Country like @SearchCountry + '%'";
                    cmd.Parameters.AddWithValue("@SearchCountry", prefixText);
                    cmd.Connection = conn;
                    conn.Open();
                    List<string> Countries = new List<string>();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            Countries.Add(sdr["Country"].ToString());
                        }
                    }
                    conn.Close();
                    return Countries;
                }
            }
        }

        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
        public static List<string> SearchRegion(string prefixText,int count)
        {
            using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = ConfigurationManager
                        .ConnectionStrings["Jobs_DBConnectionString"].ConnectionString;
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select * from Reigon where Country = @Country AND " +
                    "Reigon like @SearchRegion + '%'";
                    cmd.Parameters.AddWithValue("@Country", txtCountry.text);
                    cmd.Parameters.AddWithValue("@SearchRegion", prefixText);
                    cmd.Connection = conn;
                    conn.Open();
                    List<string> Reigons = new List<string>();
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            Reigons.Add(sdr["Reigon"].ToString());
                        }
                    }
                    conn.Close();
                    return Reigons;
                }
            }
        }
    }
}


The error:
Error 1 An object reference is required for the non-static field, method, or property 'WebApplication2.WebForm1.txtCountry' C:\Users\Abdul\Documents\Visual Studio 2010\Projects\WebApplication2\WebApplication2\WebForm1.aspx.cs 62 61 WebApplication2
Posted
Updated 31-Oct-13 5:59am
v2

You have to send additional parameters to the Static Method using UseContextKey = "true".

See the Example - ASP.NET AJAX AUTOCOMPLETEEXTENDER: PASS ADDITIONAL PARAMETER TO WEBMETHOD USING CONTEXTKEY[^]
 
Share this answer
 
C#
conn.ConnectionString = ConfigurationManager.ConnectionStrings["Jobs_DBConnectionString"].ConnectionString;


You are trying to access an object in a static method. To make this work in a static method, try passing the connection string as a parameter.
 
Share this answer
 
hi
i tried to make it like that but the second method wont work . but the probelm was in this line of code

C#
cmd.Parameters.AddWithValue("@Country", txtCountry.text);


i can not access to txtCountry

i don't think it's from the connection string

thank you
 
Share this answer
 
Comments
Please delete this as this is not an answer. If you want to comment to any answer, then click on Have a Question or Comment in that answer box itself.
You should consider passing the textbox text value as a parameter in your static webmethod since accessing the textbox.Text needs to be an instance method.

So you can add one for parameter into your static method as

C#
public static List<string> SearchRegion(string prefixText,int count,string Country)
{
....
}


By the way its txtCountry.Text not txtCountry.text

Hope this helps
 
Share this answer
 
v2
hi
i did this but then nothing showing in txtRegion because string Country parameter is null.i think it has to take a value from txtCountry.


thank you
 
Share this answer
 
Comments
Please delete this as this is not an answer. If you want to comment to any answer, then click on Have a Question or Comment in that answer box itself.
Thank you so much
finally get it to work ... can i add third parameter ??? lets see
 
Share this answer
 
hi
i tried to add the third parameter but i can't. do you have any idea ??
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900