0

hello everyone i am using two buttons on same asp.net webpage.both contain different codes first button fetches the data from database here is the code

protected void Button1_Click(object sender, EventArgs e)
{
    string username = Request.QueryString["username"];
    SqlConnection conn = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=swa1;User Id=swa1;Password=swa1;");
    conn.Open();

    try 
    {
        string checkaddress = "select address,city,zipcode from regforswa where username=" + username;
        SqlCommand com = new SqlCommand(checkaddress, conn);
        using (var reader = com.ExecuteReader())
        {
            while (reader.Read())
            {
                var tmp = reader["address"];
                if (tmp != DBNull.Value)
                {
                    laddress.Visible = true;
                    laddress.Text = reader["address"].ToString();
                }
                var cty = reader["city"];
                if (cty != DBNull.Value)
                {
                    lcity.Visible = true;
                    lcity.Text = reader["city"].ToString();
                }
                var zip = reader["zipcode"];
                if (zip != DBNull.Value)
                {
                    lzipcode.Visible = true;
                    lzipcode.Text = reader["zipcode"].ToString();
                }
            }
        }
    }
    finally
    {
        conn.Close();
    }
}

second button updates the value in the database using textbox values here is the code

protected void submit_Click(object sender, EventArgs e)
{
    string username = Request.QueryString["username"];
    string address=TextBox4.Text;
    string city=TextBox5.Text;
    string zipcode=TextBox6.Text;
    SqlConnection conn = new SqlConnection("Data Source=ADMIN-PC\\SQLEXPRESS;Initial Catalog=swa1;User Id=swa1;Password=swa1;");
    conn.Open();

    try 
    {
        string updateaddress = "UPDATE regforswa SET address=@address,city=@city,zipcode=@zipcode WHERE username="+username;
       SqlCommand com = new SqlCommand(updateaddress, conn);
       com.Parameters.AddWithValue("@address",address);
       com.Parameters.AddWithValue("@city",city);
       com.Parameters.AddWithValue("@zipcode",zipcode);
       // com.Parameters.AddWithValue("@username",username);
       if (com.ExecuteNonQuery() == 1)
       {
           result.Visible = true;
           result.Text = "congradulations.your address has been changed";
       }
       else
       {
           result.Visible = true;
           result.Text = "sorry please try again"; 
       }
    }
    catch(Exception ex)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        conn.Close();
    }
}

but the problem is when i hit the first button the validation controls related to second button does not allow the page to be reloaded so i can not fetch the data.

my question is can we use two buttons on same webpage but with different functionality to perform?

3
  • I think you should put them in different <form>s. Commented Apr 3, 2014 at 18:06
  • I assume that you are using webforms. Is this the case? This question may be of use Commented Apr 3, 2014 at 18:07
  • Try url?=username=username;DROP DATABASE swa and learn how to avoid SQL injections. Commented Apr 3, 2014 at 18:15

1 Answer 1

1

I think you can use "Validation groups" to fix your problem. http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.