Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have three C#.net code, the first code and the second code execute perfectly but
the third one does not execute.

These are my codes
C#
protected void Button8_Click(object sender, EventArgs e)
    {
        //First code
        CL.search("select CorrectAnswer from CoursesTBL where CourseID= " + (int.Parse(CoursesDropList.SelectedValue.ToString())) + " and rowID=" + int.Parse(HiddenText.Text).ToString() + "");

        if (CL.dr.HasRows)
        {
            CompareAnsTextBox.Text = CL.dr["CorrectAnswer"].ToString();
        }

        //second code
        CL.cmd.Parameters.Clear();
        CL.Operatios_SP("STDET_Insert");
        
        CL.cmd.Parameters.AddWithValue("@STDID", int.Parse(STDIDText.Text.ToString()));
        CL.cmd.Parameters.AddWithValue("@Question", LabelQuestion.Text.ToString());
        CL.cmd.Parameters.AddWithValue("@Answer", int.Parse(HiddenTextRB.Text.ToString()));
        CL.cmd.Parameters.AddWithValue("@CorrAnswer", int.Parse(CompareAnsTextBox.Text.ToString()));
        CL.cmd.Parameters.AddWithValue("@rowID", int.Parse(HiddenText.Text.ToString()));
        CL.cmd.Parameters.AddWithValue("@CourseID", int.Parse(CoursesDropList.SelectedValue.ToString()));
        CL.cmd.ExecuteNonQuery();
        CL.cmd.Parameters.Clear();
        CL.conn.Close();

        //Third code
        CL.SingleSearch("select count(rowID) from CoursesTBL  WHERE CourseID=" + int.Parse(CoursesDropList.SelectedValue.ToString()) + "", CountText);
        CL.SingleSearch("select count(Answer) from STDETTBL  WHERE STDID=" + int.Parse(STDIDText.Text.ToString()) + " and CourseID=" + int.Parse(CoursesDropList.SelectedValue.ToString()) + "", STDCountText);
        PercentageLabel.Text = ((float.Parse(STDCountText.Text) / float.Parse(CountText.Text)) * 100).ToString();
    }

And these are my procedures

//Search for the first code
public void search(string s)
    {
        try
        {
            if (conn.State == ConnectionState.Open) { conn.Close(); }
            conn.Open();
            SqlCommand cmd = new SqlCommand(s, conn);
            dr = cmd.ExecuteReader();
            dr.Read();
        }
        catch
        {
            conn.Close();
        }
    }

//save for the second code
public void Operatios_SP(string Sql)
    {
        try
        {
            if (conn.State == ConnectionState.Open) { conn.Close(); }
            conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = Sql;
            cmd.CommandType = CommandType.StoredProcedure;
        }
        catch
        {
            conn.Close();
        }
    }

//Search for the third code
public void SingleSearch(string s, TextBox Value)
    {
        try
        {
            if (conn.State == ConnectionState.Open) { conn.Close(); }
            conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = s;
            Value.Text = cmd.ExecuteScalar().ToString();
        }
        catch
        {
            Value.Text = null;
           
        }
        finally { conn.Close(); }

    }

So could you guys help me find out where the problem is

What I have tried:

I have tried to close the connection sometimes from the button and sometimes from the procedurs.
Posted
Comments
Sergey Alexandrovich Kryukov 28-Jun-16 16:08pm    
Use the debugger, find out what exactly happens, and so on...
—SA
[no name] 28-Jun-16 18:47pm    
What is the error or exception and at which line ? We cannot read your mind...
Abdalazeem Babiker 29-Jun-16 4:55am    
The error comes here
PercentageLabel.Text = ((float.Parse(STDCountText.Text) / float.Parse(CountText.Text)) * 100).ToString();
and it says
Input string was not in a correct format
I think singlesearch code does not work
Beginner Luck 30-Jun-16 20:52pm    
is it that your float have to many floating point

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