Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C#
protected void lnk_Click(object sender, EventArgs e)
        {
            string connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connection);

            List<int> list = new List<int>();
            for (int i = 0; i < 11; i++)
            {
                StringCollection sc = new StringCollection();
                TextBox txtCollection = (TextBox)Page.FindControl("txt" + i);

                sc.Add(txtCollection.Text);                
           }
Posted
Updated 26-Jul-12 23:44pm
v5
Comments
Sebastian T Xavier 27-Jul-12 3:37am    
What? When? How?
Sebastian T Xavier 27-Jul-12 3:39am    
Did you debug the code? From which line you are getting exception?
2011999 27-Jul-12 4:08am    
sc.Add(txtCollection.Text);
Sebastian T Xavier 27-Jul-12 4:59am    
I didn't see that line from your code
vvsnmurty 27-Jul-12 5:51am    
what is the id of ur finding
textbox

This is one of the easiest problems, it simply means that you are de-referencing some reference which is null at that point, but it is not supposed to be null. Just run it under debugger and analyze the statement causing the exception, run it again and check all expressions involved for null. When you find it, fix it: either make sure the object is initialized before use, or check for null and don't do the operation causing exception in case of null.

Next time, provide comprehensive exception information, complete with the line of code where the exception is thrown. And always use the debugger before asking questions like that.

—SA
 
Share this answer
 
Well, I cannot see your html so this is just speculation on my part...
Do you have 11 TextBox controls named txt0, txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, txt9, and txt10?

I notice you are starting you i at 0 not 1 so you must have a txt0.

And they must be named exactly "txt" + all options of i or you will get a null Object error like you have gotten.

Another note in the code, maybe you have stripped something out but:
C#
StringCollection sc = new StringCollection();

This is declared inside of the for block and will not be available for use after it closes. I do not see you doing anything with it in the code given so I must assume you intend to do something with it after the for loop, so you need to declare the sc variable before you open the for loop.

This is variable "scope" and right now the scope is only the for loop itself.
 
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