简体   繁体   中英

c# asp.net string compare

here is my C# code

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    TextBox25.Text = DateTime.Now.ToString("MM/yy");

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["preconn"].ToString());

    con.Open();

    SqlCommand com = new SqlCommand("select * from employe Where user_id = '" + DropDownList1.SelectedItem.Value + "'", con);

    SqlDataReader reader = com.ExecuteReader();

    if (reader.Read())
    {
        TextBox1.Text = reader["Id"].ToString();
    }
    con.Close();
}

protected void Button1_Click(object sender, EventArgs e)
    {

        string user1 = Convert.ToString(TextBox25.Text);

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["preconn"].ToString());

        con.Open();

        SqlCommand com2 = new SqlCommand("select * from salary where id='" + DropDownList1.SelectedItem.Text + "'", con);

        SqlDataReader reader1 = com2.ExecuteReader();

        if (reader1.Read())
        {
            string user;
            user = Convert.ToString(reader1["month"]);

            //if (TextBox25.Text == user)
            if (user1.CompareTo(user)==0)
            {
                Label9.Visible = true;
                Label9.Text = "Salary already updated please check again";
            }
        }// user check end

        else
        {

            SqlCommand com1 = new SqlCommand("insert into salary values(@id,@work_days,@month)",con);

            SqlParameter obj1 = new SqlParameter("@id", DbType.StringFixedLength);
            obj1.Value = DropDownList1.SelectedItem.Value;
            com1.Parameters.Add(obj1);

            SqlParameter obj2 = new SqlParameter("@work_days", DbType.StringFixedLength);
            obj2.Value = TextBox10.Text;
            com1.Parameters.Add(obj2);

            SqlParameter obj15 = new SqlParameter("@month", DbType.StringFixedLength);
            obj15.Value = TextBox25.Text;
            com1.Parameters.Add(obj15);

            com1.ExecuteNonQuery();

            con.Close();


        }

    }

when i click on Button1, data is not inserted and no error message is showing. i think the problem is in my condition if (user1.CompareTo(user)==0)

i am using sql server 2008 visual studio 2010

I Think you must do something like this

 user = Convert.ToString(reader1['rowIndex']["month"]);

which RowIndex is an integer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM