Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a a Textbox_TextChanged Event with a code that will divide the number being entered. In that same TextChange I have the math code to see if the user is a Semester User or Quarter User. In the math code if the user is a Semester User it will divide be a certain number. If the user is a Quarter User it will divide by another certain number. I need another pare of eyes to look and see if the code looks ok. It is not working on my side. Can someone tell me if I have missed something or useing the IF,Else statement wrong?

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

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

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

            foreach (DataRow dr1 in dt1.Rows)
            {
                int returnedLevel = Convert.ToInt32(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);
                }
   }
Posted
Comments
Tom Marvolo Riddle 14-Nov-13 8:02am    
In which part it doesn't work?It show any error?
Computer Wiz99 14-Nov-13 8:05am    
Before I debug the code to test it, I have an error that says: "The name 'Semester' does not exist in the current context". And the same for the name Quarter. What did I not do?
Tom Marvolo Riddle 14-Nov-13 8:08am    
The value get from db in returnedlevel is semester or quarter am i right?
Computer Wiz99 14-Nov-13 8:09am    
Yes.
Tom Marvolo Riddle 14-Nov-13 8:11am    
check solution and let me know.

1 solution

If you get string value from DB.You have to change into string

C#
string returnedLevel = (dr1[0].ToString());


you have to put "" while compare.try this

C#
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);
}



or if it is int,
you have to assign a value while declaring variable
 
Share this answer
 
v3
Comments
Mike Meinz 14-Nov-13 8:14am    
Jas24,
I don't think that using Semester and Quarter as literals will work. The OP converts the value retrieved from the database to Int32 before storing it in "returnedlevel" so I expect that Semester and Quarter should be an integer constant.
Tom Marvolo Riddle 14-Nov-13 8:16am    
Sorry Mike Meinz.I forget to intimate to convert to string.please See his comments.
Mike Meinz 14-Nov-13 8:18am    
When I was in the Navy, my captain used to say "Bad information is the curse of our business". I've been cursed again!!!
Tom Marvolo Riddle 14-Nov-13 8:21am    
Is there any mistake by me?
Mike Meinz 14-Nov-13 8:27am    
No, your Solution is exactly correct. I voted 5 stars.

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