Click here to Skip to main content
14,693,599 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a gridview populating from Json deserialization. Once click the button download, the json string will be displayed as gridview in the page. I can select any gridview row using chekbox and click send to store the data in my local db.

Now, when I click the button download again, I dont want the data already stored in my db to appear is the gridview again. How to do this code checking?

Here is my code :

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;
        }
    }
}


What I have tried:

//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;
            }
Posted
Updated 24-Aug-20 21:31pm

1 solution

Quote:
How to do this code checking?
Maybe you need an additional column on the table in your ?remote database.

When you 'download' and 're-store' the data in your local db (your terms) you update the column in the ? remote table/db to say that row/data has already been updated.

You then update your code for the 'button download' to select the rows where the 'flag' for downloaded already is not set.

Taking this further, if you used a datetime field on the db, you could then at some later stage, if there was an issue of 'consistency', use the datetime field to determine WHEN the original download was initiated.

.. if you can't do it this way, then it would be painful, but you could

a) get a list of 'ids' from the local db, ie, rows that have been processed

b) construct the download query to have a 'AND id NOT in [list]'

(but this is a messier option for lots of reasons)
   

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