Click here to Skip to main content
15,900,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I havs a TextBox_TextChange depending on the user Level it will do the math function that is set up for that user. I have an If Else statement in place. When I enter a number into a textbox and hit tab or click ti the next textbox it should fire the Textbox_Textchange and fire the math operators if the user has semester level or a quarter level. The math operator or If Else statement is not working. What is wrong with the code?

C#
protected void TextBoxNCC_TextChanged(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
       con.Open();

       if (true)
       {
           SqlCommand level = new SqlCommand("Select INST_ID, Calendar from Table44 where INST_ID = @INST_ID", con);
           level.Parameters.Add(new SqlParameter("INST_ID", TextBoxINST_ID.Text));


           SqlDataReader reader = level.ExecuteReader();
           DataTable dt1 = new DataTable();
           dt1.Load(reader);

           foreach (DataRow dr1 in dt1.Rows)
           {
              string returnedLevel = (dr1[0].ToString());
               int inst_id = Convert.ToInt32(dr1[2].ToString());
               Session["inst_id"] = inst_id;

               if (returnedLevel == "Semester")
              {
                  int i = Convert.ToInt32(TextBoxNCC.Text);
                  int j = 168;
                  TextBoxNCCDR.Text = Convert.ToString(i / j);

              }
              else if (returnedLevel == "Quarter")
              {
                  int i = Convert.ToInt32(TextBoxNCC.Text);
                  int j = 120;
                  TextBoxNCCDR.Text = Convert.ToString(i / j);
              }
               }
           }
       }
   }


ASP.NET
<td class="style27">
                <asp:TextBox ID="TextBoxNCC" runat="server" Width="180px" 
                    ontextchanged="TextBoxNCC_TextChanged" AutoPostBack="True"></asp:TextBox>
            </td>
Posted
Updated 14-Nov-13 3:21am
v2
Comments
ZurdoDev 14-Nov-13 9:12am    
Do you have your textbox set to autopostback?
Computer Wiz99 14-Nov-13 9:20am    
Yes, I will show it to you.
ZurdoDev 14-Nov-13 9:23am    
So, put a breakpoint in your page_load and in your TextBoxNCC_TextChanged event and see if they fire. Put the breakpoint on the first line.
Computer Wiz99 14-Nov-13 9:23am    
I get this error when I am testing it. "Cannot find column 2." and this is what the error is coming from "int inst_id = Convert.ToInt32(dr1[2].ToString());"
Kornfeld Eliyahu Peter 14-Nov-13 9:25am    
There are 2 columns but they numbered 0 based so it should be dr1[1]...

1 solution

drl[2] does not exist because it is 0 based and you are only selecting 2 columns. You want to use drl[0] instead.
 
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