Сравните gridview и значение базы данных
Привет,
У меня есть gridview, заполняющийся из десериализации Json. Как только вы нажмете кнопку Загрузить, строка json будет отображаться на странице в виде gridview. Я могу выбрать любую строку gridview с помощью chekbox и нажать кнопку Отправить, чтобы сохранить данные в моей локальной БД.
Теперь, когда я снова нажимаю кнопку Загрузить, я не хочу, чтобы данные, уже хранящиеся в моей БД, снова появлялись в gridview. Как сделать эту проверку кода?
Вот мой код :
public partial class HQSelectionPage : System.Web.UI.Page { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); protected void Page_Load(object sender, EventArgs e) { ddCompany.Visible = false; btnSendOrder.Visible = false; lblSelectCompany.Visible = false; LoadCompany(); } protected void BtnDownloadOrder_Click(object sender, EventArgs e) { //Get json from shopify url string shopifyurl = String.Format("https://94714252f343a45bf2a34008068cac00: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@wingheong-test.myshopify.com/admin/api/2020-07/orders.json?fullfillment_status=unshipped"); //To get .Net Framework 4.0 to run on newer version of TLS ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; WebRequest requestObject = WebRequest.Create(shopifyurl); requestObject.Credentials = new NetworkCredential("94714252f343a45bf2a34008068cac00", "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"); requestObject.Method = "GET"; HttpWebResponse responseObjGet = null; responseObjGet = (HttpWebResponse)requestObject.GetResponse(); //To read the strem from the response Object String shopifyresult = null; using (Stream stream = responseObjGet.GetResponseStream()) { StreamReader sr = new StreamReader(stream); //Convert JSON to string shopifyresult = sr.ReadToEnd(); sr.Close(); } var serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; GetShopifyOrder st = JsonConvert.DeserializeObject<GetShopifyOrder>(shopifyresult, settings); foreach (var obj in st.orders) { long id = obj.id; string created_at = obj.created_at; double total_price = obj.total_price; string email = obj.email; string name = obj.name; double total_tax = obj.total_tax; long total_weight = obj.total_weight; string note = obj.note; string first_name = obj.customer.first_name; string last_name = obj.customer.last_name; string code = obj.shipping_lines[0].code; double price = obj.shipping_lines[0].price; //string province = obj.shipping_address.province; //long zip = obj.shipping_address.zip; //string phone = obj.shipping_address.phone; } //bind the data in gridview grvOrders.DataSource = st.orders; grvOrders.DataBind(); //Make button visible ddCompany.Visible = true; btnSendOrder.Visible = true; lblSelectCompany.Visible = true; } protected void BtnSendOrder_Click(object sender, EventArgs e) { //save into local db foreach (GridViewRow gvrow in grvOrders.Rows) { var checkbox = gvrow.FindControl("CheckBox1") as CheckBox; if (checkbox.Checked) { var lblID = gvrow.Cells[1].Text; var lblcreatedat = gvrow.Cells[2].Text; var lbltotalprice = gvrow.Cells[3].Text; var lblemail = gvrow.Cells[4].Text; var lblname = gvrow.Cells[5].Text; var lbltotal_tax = gvrow.Cells[6].Text; var lbltotal_weight = gvrow.Cells[7].Text; var lblnote = gvrow.Cells[8].Text; var lblfirst_name = gvrow.Cells[9].Text; var lbllast_name = gvrow.Cells[10].Text; var lblcode = gvrow.Cells[11].Text; var lblprice = gvrow.Cells[12].Text; //var lblprovince = gvrow.Cells[13].Text; //var lblzip = gvrow.Cells[14].Text; //var lblphone = gvrow.Cells[15].Text; string companyid = ddCompany.SelectedValue; //SqlCommand cmd = new SqlCommand("Insert into tblOrders values (@id,@createdat,@totalprice,@email,@name,@total_tax,@total_weight,@note,@first_name,@last_name,@code,@price,@province,@zip,@phone,@companyid)", con); SqlCommand cmd = new SqlCommand("Insert into tblOrders values (@id,@createdat,@totalprice,@email,@name,@total_tax,@total_weight,@note,@first_name,@last_name,@code,@price,@companyid)", con); cmd.Parameters.AddWithValue("@id", lblID); cmd.Parameters.AddWithValue("@createdat", Convert.ToDateTime(lblcreatedat)); cmd.Parameters.AddWithValue("@totalprice", lbltotalprice); cmd.Parameters.AddWithValue("@email", lblemail); cmd.Parameters.AddWithValue("@name", lblname); cmd.Parameters.AddWithValue("@total_tax", lbltotal_tax); cmd.Parameters.AddWithValue("@total_weight", lbltotal_weight); cmd.Parameters.AddWithValue("@note", lblnote); cmd.Parameters.AddWithValue("@first_name", lblfirst_name); cmd.Parameters.AddWithValue("@last_name", lbllast_name); cmd.Parameters.AddWithValue("@code", lblcode); cmd.Parameters.AddWithValue("@price", lblprice); //cmd.Parameters.AddWithValue("@province", lblprovince); //cmd.Parameters.AddWithValue("@zip", lblzip); //cmd.Parameters.AddWithValue("@phone", lblphone); cmd.Parameters.AddWithValue("@companyid", companyid); con.Open(); int i = cmd.ExecuteNonQuery(); con.Close(); ddCompany.Visible = true; btnSendOrder.Visible = true; lblSelectCompany.Visible = true; ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "ClientScript", "alert('Orders assigned successfully')", true); //redirect Response.Redirect("~/Account/HQSelectionPage"); } } } protected void LoadCompany() { if (!IsPostBack) { using (UnitOfWork uow = new UnitOfWork()) { var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>(); var items = uow.Repository<tblComp>().Table.Select(x => x.CompName).ToList(); ddCompany.DataSource = items; ddCompany.DataBind(); ddCompany.Items.Insert(0, "--Select target branch--"); } } } protected void ddCompany_SelectedIndexChanged(object sender, EventArgs e) { ddCompany.Visible = true; btnSendOrder.Visible = true; lblSelectCompany.Visible = true; } } }
Что я уже пробовал:
//To disable the row which is already assigned List<string> alIDList = new List<string>(); foreach (GridViewRow row in grvOrders.Rows) { string strID = row.Cells[1].Text; alIDList.Add(strID); } string combindedString = string.Join(",", alIDList); SqlConnection conn3 = con; string sql3 = null; conn3.Open(); sql3 = "SELECT ID, (SELECT COUNT(*) FROM tblOrders where ID IN (SELECT state_split FROM [dbo].nom_return_splited_val_fn(@ids,','))) As count FROM tblOrders where ID in (SELECT state_split FROM [dbo].nom_return_splited_val_fn(@ids,','))"; SqlCommand cmd3 = new SqlCommand(sql3, conn3); cmd3.Parameters.AddWithValue("@ids", combindedString); string iDs = (string)cmd3.ExecuteScalar(); conn3.Close(); foreach (GridViewRow row in grvOrders.Rows) { iDs != row.Cells[1].Text; }