В gridview, как редактировать, проверять и обновлять три текстовых поля подряд - PullRequest
0 голосов
/ 28 февраля 2020
protected void grvtbl_UserMaster_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //DataTable dt = new DataTable();
        Label lblId = (Label)grvtbl_UserMaster.Rows[e.RowIndex].FindControl("lblId");
        TextBox txteditUSERNAME = (TextBox)grvtbl_UserMaster.Rows[e.RowIndex].FindControl("txteditUSERNAME");
        SqlCommand com = new SqlCommand("select * from  tbl_UserMaster where UserName = @username", con);
        com.CommandType = CommandType.Text;
        com.Parameters.AddWithValue("@username", txteditUSERNAME.Text.ToString());
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(com);
        sda.Fill(dt);
        if (dt.Rows.Count >= 1)
        {
            Label lblusername = (Label)grvtbl_UserMaster.Rows[e.RowIndex].FindControl("lblId");
     TextBox txteditemailid = (TextBox)grvtbl_UserMaster.Rows[e.RowIndex].FindControl("txteditemailid");
            SqlCommand com1 = new SqlCommand("select * from  tbl_UserMaster where Email = @Email", con);
            com1.CommandType = CommandType.Text;
            com1.Parameters.AddWithValue("@Email", txteditemailid.Text.ToString());
            DataTable dt1 = new DataTable();
            SqlDataAdapter sda1 = new SqlDataAdapter(com1);
            sda1.Fill(dt1);
            if (dt1.Rows.Count >= 1)
            {
                alert.callAlert("warning", "", "Email_Id Already Exists");
                grvtbl_UserMaster.EditIndex = -1;
                BindExistingUser();
            }
            else
            {
     SqlCommand cmd1 = new SqlCommand(" Update tbl_UserMaster set Email = @Email where UserId=@Id", con);
                cmd1.CommandType = CommandType.Text;
                cmd1.Parameters.Add("@Id", SqlDbType.VarChar).Value = lblId.Text;
                cmd1.Parameters.AddWithValue("@Email", txteditemailid.Text.ToString());
                con.Open();
                int i = cmd1.ExecuteNonQuery();
                con.Close();
                if (i > 0)
                {
                    alert.callAlert("success", "", "Email_Id Updated Successfully");
                    grvtbl_UserMaster.EditIndex = -1;
                    BindExistingUser();
                    return;
                }
            }
        }
        if (dt.Rows.Count >= 1)
        {
            alert.callAlert("warning", "", "User Name  Already Exists");
            grvtbl_UserMaster.EditIndex = -1;
            BindExistingUser();
        }
        else
        {

     SqlCommand cmd1 = new SqlCommand(" Update tbl_UserMaster set UserName=@Name where UserId=@Id", con);
            cmd1.CommandType = CommandType.Text;
            cmd1.Parameters.Add("@Id", SqlDbType.VarChar).Value = lblId.Text;
            cmd1.Parameters.Add("@Name", SqlDbType.VarChar).Value = txteditUSERNAME.Text.Trim();
            //cmd1.Parameters.AddWithValue("@Email", txteditemailid.Text.ToString());
            con.Open();
            int i = cmd1.ExecuteNonQuery();
            con.Close();
            if (i > 0)
            {
                alert.callAlert("success", "", "User Name Updated Successfully");
                grvtbl_UserMaster.EditIndex = -1;
                BindExistingUser();
            }
        }
    }
...