Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
string url = string.Empty;
                bool isAgri = true;
                int count = 0; //added
                foreach (GridViewRow row in grdRpt.Rows)
                {
                    CheckBox chk = (CheckBox)grdRpt.Rows[row.RowIndex].FindControl("chkselecdata");
                    if (chk.Checked == true)
                    {
                        count++;
                        int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
                        if (row.Cells[4].Text == "Agri Input")
                        {
                            isAgri = true;
                            url = ConfigurationManager.AppSettings["SAPAgriInputPostingUrl"].ToString();
                        }
                        break;
                    }
                }

String strConnString = ConfigurationManager.ConnectionStrings["IvcSpiderDBConnectionStrings"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                if (count > 0)
                {
                    if (isAgri)
                    {
                        string slip_No = string.Empty;
                        List<string> slipnoList = new List<string>();
                        List<Dist_Request> dist_requests = new List<Dist_Request>();
                        foreach (GridViewRow row in grdRpt.Rows)
                        {
                            CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
                            if (cb.Checked == true)
                            {
                                int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);

                                slip_No = row.Cells[2].Text;
                                Dist_Request dist_request = new Dist_Request();
                                LT_ITEMS lT_ITEM = new LT_ITEMS();
   lT_ITEM.MATNR = row.Cells[15].Text == " " ? string.Empty : row.Cells[15].Text;
   
  dist_request = dist_requests.Where(r => r.SLIP_NO == slip_No).FirstOrDefault();
             if (dist_request != null && dist_request.SLIP_NO != null)
               {
dist_requests.Find(f => f.SLIP_NO == slip_No).PostingNav.Add(lT_ITEM);
dist_requests.Find(f => f.SLIP_NO == slip_No).FLAG += "," + key.ToString();
               }
      else
             {
                  dist_request = new Dist_Request();
                  dist_request.SLIP_NO = slip_No;
                  dist_request.BUDAT = DateTime.Now.ToString("yyyyMMdd");
                  dist_request.MBLNR = string.Empty; 
                            dist_request.FLAG = key.ToString();
                           List<LT_ITEMS> lT_ITEMs = new List<LT_ITEMS>();
                           List<ReturnNav> listreturnNav = new List<ReturnNav>();
                           lT_ITEMs.Add(lT_ITEM);
                          dist_request.PostingNav = lT_ITEMs;
                          dist_request.ReturnNav = listreturnNav;
                          dist_requests.Add(dist_request);
            }

 }
                            else
                            {
                                slip_No = row.Cells[2].Text;
                                slipnoList.Add(slip_No);
                            }
                        }
                        HttpHelper httpHelper = new HttpHelper();
                        if (dist_requests.Count > 1)
                        {
                            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('More than one slip no selected , Cannot post with multiple slip no');", true);
                            return;
                        }

        string reqkeys = dist_requests.FirstOrDefault().FLAG;
                        dist_requests.FirstOrDefault().FLAG = "1";
                        Dist_Response dist_Response = new Dist_Response();

               dist_Response = httpHelper.PostAsync<Dist_Response, 
         Dist_Request>(dist_requests.FirstOrDefault(), url).GetAwaiter().GetResult();
                List<int> updateKeys = reqkeys.Split(',').Select(int.Parse).ToList();
                        string resulttype = "E";
                        string resultmsg = "";
                        string alertmsg = "Unable to Post in SAP ";
if (dist_Response != null && dist_Response.d != null && dist_Response.d.ReturnNav != null && dist_Response.d.ReturnNav.results != null)
                        {
             foreach (ReturnNavresults r in dist_Response.d.ReturnNav.results)
                            {
                                resulttype += "|" + r.Type;
                                resultmsg += "|" + r.Type + ":" + r.Message;
                            }
                            if (!string.IsNullOrEmpty(resultmsg))
                            {
                                resulttype.Remove(0, 1);
                                resultmsg.Remove(0, 1);
                                alertmsg = resultmsg;
                            }
                            resulttype = dist_Response.d.ReturnNav.results.FirstOrDefault().Type;
                            resultmsg = dist_Response.d.ReturnNav.results.FirstOrDefault().Message;
                            alertmsg = resultmsg;
                        }
                        foreach (int key in updateKeys)
                        {
                            SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = '" + resulttype + "',status_description = '" + resultmsg + "',posteddate = convert(varchar, getdate(), 103),username = '" + Request.Cookies["SpiderUserLoginName"].Value.ToString() + "' Where transaction_id=" + key.ToString(), con);
                            cmd.ExecuteNonQuery();
                            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('" + alertmsg + "');", true);
                        }
                    }


In run mode gridview as follows


slipno slipdate season location zone
2315 28.05.2018 2019 1001 Diawala
2315 28.05.2018 2019 1002 Ferke
2315 28.05.2018 2019 1003 Fereskdo

i want to do one validation in gridview if suppose one slip no have appear more than once means, i want to show message to the user "Some more slipno is there do you proceed?"
ok Cancel

for that how to do in asp.net using c#

What I have tried:

C#
string url = string.Empty;
                bool isAgri = true;
                int count = 0; //added
                foreach (GridViewRow row in grdRpt.Rows)
                {
                    CheckBox chk = (CheckBox)grdRpt.Rows[row.RowIndex].FindControl("chkselecdata");
                    if (chk.Checked == true)
                    {
                        count++;
                        int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
                        if (row.Cells[4].Text == "Agri Input")
                        {
                            isAgri = true;
                            url = ConfigurationManager.AppSettings["SAPAgriInputPostingUrl"].ToString();
                        }
                        break;
                    }
                }

<pre> String strConnString = ConfigurationManager.ConnectionStrings["IvcSpiderDBConnectionStrings"].ConnectionString;
                SqlConnection con = new SqlConnection(strConnString);
                con.Open();
                if (count > 0)
                {
                    if (isAgri)
                    {
                        string slip_No = string.Empty;
                        List<string> slipnoList = new List<string>();
                        List<Dist_Request> dist_requests = new List<Dist_Request>();
                        foreach (GridViewRow row in grdRpt.Rows)
                        {
                            CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
                            if (cb.Checked == true)
                            {
                                int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);

                                slip_No = row.Cells[2].Text;
                                Dist_Request dist_request = new Dist_Request();
                                LT_ITEMS lT_ITEM = new LT_ITEMS();
   lT_ITEM.MATNR = row.Cells[15].Text == " " ? string.Empty : row.Cells[15].Text;
   
  dist_request = dist_requests.Where(r => r.SLIP_NO == slip_No).FirstOrDefault();
             if (dist_request != null && dist_request.SLIP_NO != null)
               {
dist_requests.Find(f => f.SLIP_NO == slip_No).PostingNav.Add(lT_ITEM);
dist_requests.Find(f => f.SLIP_NO == slip_No).FLAG += "," + key.ToString();
               }
      else
             {
                  dist_request = new Dist_Request();
                  dist_request.SLIP_NO = slip_No;
                  dist_request.BUDAT = DateTime.Now.ToString("yyyyMMdd");
                  dist_request.MBLNR = string.Empty; 
                            dist_request.FLAG = key.ToString();
                           List<LT_ITEMS> lT_ITEMs = new List<LT_ITEMS>();
                           List<ReturnNav> listreturnNav = new List<ReturnNav>();
                           lT_ITEMs.Add(lT_ITEM);
                          dist_request.PostingNav = lT_ITEMs;
                          dist_request.ReturnNav = listreturnNav;
                          dist_requests.Add(dist_request);
            }

 }
                            else
                            {
                                slip_No = row.Cells[2].Text;
                                slipnoList.Add(slip_No);
                            }
                        }
                        HttpHelper httpHelper = new HttpHelper();
                        if (dist_requests.Count > 1)
                        {
                            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('More than one slip no selected , Cannot post with multiple slip no');", true);
                            return;
                        }

        string reqkeys = dist_requests.FirstOrDefault().FLAG;
                        dist_requests.FirstOrDefault().FLAG = "1";
                        Dist_Response dist_Response = new Dist_Response();

               dist_Response = httpHelper.PostAsync<Dist_Response, 
         Dist_Request>(dist_requests.FirstOrDefault(), url).GetAwaiter().GetResult();
                List<int> updateKeys = reqkeys.Split(',').Select(int.Parse).ToList();
                        string resulttype = "E";
                        string resultmsg = "";
                        string alertmsg = "Unable to Post in SAP ";
if (dist_Response != null && dist_Response.d != null && dist_Response.d.ReturnNav != null && dist_Response.d.ReturnNav.results != null)
                        {
             foreach (ReturnNavresults r in dist_Response.d.ReturnNav.results)
                            {
                                resulttype += "|" + r.Type;
                                resultmsg += "|" + r.Type + ":" + r.Message;
                            }
                            if (!string.IsNullOrEmpty(resultmsg))
                            {
                                resulttype.Remove(0, 1);
                                resultmsg.Remove(0, 1);
                                alertmsg = resultmsg;
                            }
                            resulttype = dist_Response.d.ReturnNav.results.FirstOrDefault().Type;
                            resultmsg = dist_Response.d.ReturnNav.results.FirstOrDefault().Message;
                            alertmsg = resultmsg;
                        }
                        foreach (int key in updateKeys)
                        {
                            SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = '" + resulttype + "',status_description = '" + resultmsg + "',posteddate = convert(varchar, getdate(), 103),username = '" + Request.Cookies["SpiderUserLoginName"].Value.ToString() + "' Where transaction_id=" + key.ToString(), con);
                            cmd.ExecuteNonQuery();
                            ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('" + alertmsg + "');", true);
                        }
                    }



In run mode gridview as follows


slipno slipdate season location zone
2315 28.05.2018 2019 1001 Diawala
2315 28.05.2018 2019 1002 Ferke
2315 28.05.2018 2019 1003 Fereskdo

i want to do one validation in gridview if suppose one slip no have appear more than once means, i want to show message to the user "Some more slipno is there do you proceed?"
ok Cancel

for that how to do in asp.net using c#
Posted
Updated 24-Oct-18 10:18am
v2
Comments
Richard MacCutchan 24-Oct-18 6:26am    
"for that how to do in asp.net using c#"
You don't. The C# code runs in the server not in the browser, which is where the user interacts with the web site. You need to pass some information back to your Javascript and post the alert message there.

But first you need to fix your SQL as suggested by OriginalGriff - and not for the first time. See to this as a matter of urgency to protect the integrity of your database.

1 solution

Do not do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that throughout your app first as a matter of priority, then consider the problem you are having. But miss one ... and your DB will be damaged.
 
Share this answer
 

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