Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web form in Asp.net using C#. I have a code that checks to see if a record exist in a table depending on the year. If that record exist the Submit button hidden and if it does not exist the Submit button is shown. That code worked great. Now I have updated that table with a column named Submit. In that column it will be True or False. How can I get my code to check to see if the record exist depending on the Year and the Submit to show or hide the Submit button?

Example:

1. James Hammer 2015 False
2. James Black 2015 True

If Submit = False, Show Submit Button
If Submit = True, Hide Submit Button

Here is my code:

C#
 SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                    con.Open();

const string cmdStr = "Select count(*) from Table44 where User_ID= @User_ID AND YEAR = 2015 AND SUBMIT = False";
                    SqlCommand userExist = new SqlCommand(cmdStr, con7);
                    userExist.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);
                    userExist.Parameters.AddWithValue("@YEAR", TextBoxDATE2.Text);
                    userExist.Parameters.AddWithValue("@SUBMIT", TextBoxSUB.Text);
                    int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
                    if (temp == 0)
                    {
                        ButtonSubmit.Visible = true;
                        
                    }
                    else if (temp == 1)
                    {
                        ButtonSubmit.Visible = false;
                        
                    }
Posted
Updated 5-May-15 3:54am
v3

Please put your connection string :-
C#
SqlConnection con7 = new SqlConnection();
con7.ConnectionString = "Please put your ConnectionString here";
const string cmdStr = "Select count(*) from Table44 where User_ID= @User_ID AND YEAR = 2015 AND SUBMIT = @SUBMIT";
SqlCommand userExist = new SqlCommand(cmdStr, con7);
userExist.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);
userExist.Parameters.AddWithValue("@YEAR", TextBoxDATE2.Text);
userExist.Parameters.AddWithValue("@SUBMIT", TextBoxSUB.Text);
if (con7.State == ConnectionState.Open)	{ con7.Close(); }

con7.Open();
object ans = userExist.ExecuteScalar();
con7.Close();
int temp = Convert.ToInt32(ans.ToString());
if (temp == 0)
{
	ButtonSubmit.Visible = true;
}
else if (temp == 1)
{
	ButtonSubmit.Visible = false;
}
 
Share this answer
 
v2
Comments
Computer Wiz99 5-May-15 9:56am    
Psima, Thanks but the connection was not my issue for this not to work. Right now it just checks to see if the record exists. How can I have it to check to see if a value exist and depending on the value the other code works?
Hi,

At my side below code is working fine.

string sql = "select query";
SqlConnection con = new SqlConnection("data connection");
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();

if(count != 0)
{
//do this string
}
else
{

//do this one
}


Please try and let me know.

Thanks in advance.
 
Share this answer
 
I did it this way. Thanks for all your help.

C#
SqlConnection con7 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                    con7.Open();

                    SqlCommand level = new SqlCommand("Select User_ID, SUBMITTED, FINYR from Table99 where User_ID = @User_ID AND FINYEAR = 2015", con7);
                    level.Parameters.Add(new SqlParameter("User_ID", TextBoxUser_ID.Text));
                    level.Parameters.Add(new SqlParameter("SUBMITTED", TextBoxSUB.Text));

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

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


                        if (returnedLevel == "False")
                        {
                            ButtonSubmit.Visible = true;
                            //ButtonPrint.Visible = false;
                        }
                        else if (returnedLevel == "True")
                        {
                            ButtonSubmit.Visible = false;
                            //ButtonPrint.Visible = true;
                        }
                        con7.Close();
                        reader3.Close();
 
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