Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,


I would like to create a lookup function just like this Example . But i dont know much about vb and i dont see much information about the workflow of that Lookup . so its hard for me to understand .Now My project needs this kind of Look up to be done on C#. Please Help . I have tried with my own logic . Please see my code Below and it shows error as "
C#
object is not set to an instance of an object

"

What I have tried:

C#
public static void fill(string property)
        {
            Page page = (Page)HttpContext.Current.Handler;
            TextBox TenancyAddress = (TextBox)page.FindControl("TenancyAddress");
            TenancyAddress.Text = property;
        }

        [WebMethod]
        public static void LookupUpdate(string id)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from [godaddy].[dbo].[Property] where [Prop_id] = " + id, con);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
                Page page = (Page)HttpContext.Current.Handler;
                TextBox TenancyAddress = (TextBox)page.FindControl("TenancyAddress");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                 string property = ds.Tables[0].Rows[i]["PropertyName"].ToString();
                    fill(property);
                }
            }
            con.Close();
            



        }
Posted
Updated 30-May-16 16:07pm
v2
Comments
Which line?
Member 11683406 30-May-16 18:19pm    
Hi this line am getting error TenancyAddress.Text = property;
CHill60 30-May-16 18:46pm    
Put a breakpoint on TextBox TenancyAddress = (TextBox)page.FindControl("TenancyAddress"); ... you will find that TenancyAddress is NULL. You do not have a control on your page called "TenancyAddress" ... check for the exact spelling (including capitals)
Member 11683406 31-May-16 2:31am    
Hai Chill your are correct the TenancyAddress is Null . But in my Page Tenancy address is a asp textbox and when am directly set the text property like this TenancyAddress.Text = property; it shows error as an object reference is required to access non-static member
CHill60 31-May-16 3:00am    
Check your markup for the exact spelling of the name property on the textbox and also confirm that you have runat=server on the textbox

1 solution

CHill60 is correct. The FindControl could not able to find the control on page. That means it does not exist.

Why can't you directly access the control with Id? If you have runat=server declared on the control, then you can access it directly on code behind.
 
Share this answer
 
Comments
CHill60 31-May-16 4:42am    
OP has clarified that the error is actually "error as an object reference is required to access non-static member". I think they might need to pass the textbox into the WebMethod OR return the value back to the javascript function. I'm moving out of my sphere of knowledge now!
Oh thanks for the info. Yes he has to return the data back.

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