views:

25

answers:

0

protected void Authorise(object sender, EventArgs e) { CheckBox chk;

        foreach (GridViewRow rowItem in GridView1.Rows)
        {
           // gets the Debit Reference number for each row checked
            string type = GridView1.DataKeys[rowItem.RowIndex].Values[0].ToString();
            // gets the Debit Error Type Number number for each row checked
            string DDIREF = GridView1.DataKeys[rowItem.RowIndex].Values[1].ToString();

            chk = (CheckBox)(rowItem.Cells[0].FindControl("RowChecker"));

            // chk.checked will access the checkbox state on button click event
            if (chk.Checked)
            {

                 if (type == "1")
                 {
                    // Message_ltr.Text = type + DDIREF;
                     string[] posList;
                     //   string a = "false";
                     DataTable dt = this.ExcelToDatatable_dt;

                     posList = positionList;
                     foreach (DataRow row in dt.Rows)
                     {
                             string error = "No Error Message";
                             fn.InsertNewDDIsTemporary_Proc(
                                      row["DDIREF"].ToString()                    //  String DDIREF_P
                                    , row["Sortcode"].ToString()                  //  String Sortcode_P
                                    , row["AccountNo"].ToString()                 //  String AccountNo_P
                                    , row["AccountName"].ToString()               //  String AccountName_P
                                    , strCompanyID_V                              //  String CompanyID_P
                                    , (posList[5].Length > 0) ? row["CompanyName"].ToString() : "-"                //  String CompanyName_P
                                    , (posList[6].Length > 0) ? row["ADD1"].ToString() : "-"                      //  String ADD1_P
                                    , (posList[7].Length > 0) ? row["ADD2"].ToString() : "-"                   //  String ADD2_P
                                    , (posList[8].Length > 0) ? row["ADD3"].ToString() : "-"                    //  String ADD3_P
                                    , (posList[9].Length > 0) ? row["ADD4"].ToString() : "-"                      //  String ADD4_P
                                    , (posList[10].Length > 0) ? row["Postcode"].ToString() : "-"                  //  String Postcode_P
                                    , (posList[11].Length > 0) ? row["email"].ToString() : "-"                     //  String email_P
                                    , (posList[12].Length > 0) ? row["CollectionDay"].ToString() : "-"             //  String CollectionDay_P
                                    , (posList[13].Length > 0) ? row["phone"].ToString() : "-"                    //  String phone_P
                                    , (posList[14].Length > 0) ? row["fax"].ToString() : "-"                      //  String fax_P
                                    , (posList[15].Length > 0) ? row["Notification"].ToString() : "-"             //  String Notification_P
                                    , (posList[16].Length > 0) ? row["ContactName"].ToString() : "-"               //  String ContactName_P
                                    , (posList[17].Length > 0) ? row["Companycode"].ToString() : "-"              //  String Companycode_P
                                    , (posList[19].Length > 0) ? row["First_Amount"].ToString() : "-"              //  String First_Amount_P
                                    , (posList[20].Length > 0) ? row["Monthly_amount"].ToString() : "-"           //  String Monthly_amount_P
                                    , (posList[21].Length > 0) ? row["Total_amount"].ToString() : "-"             //  String Total_amount_P
                                    , (posList[22].Length > 0) ? row["NO_TRANS"].ToString() : "-"                //  String NO_TRANS_P
                                    , (posList[23].Length > 0) ? row["FREQUENCY"].ToString() : "-"                 //  String FREQUENCY_P
                                    , DateTime.Now //(posList[24].ToString()) ?? row["First_Collection_Date"]     //  String First_Collection_Date_P
                                    , DateTime.Now              //  String dateOfBirth_P
                                    , null             //  String P_REPEATED_F_P
                                    , DateTime.Now    //  String second_collection_date_P
                                    , type                  //  String type_P
                                    , (posList[27].Length > 0) ? row["MOBILE"].ToString() : "-"                  //  String MOBILE_P
                                  , error);

                     }

                }
                                    else

                                        {

                                            Message_ltr.Text = " Cannot Authorise";
                                        }

             }
        }

    }

What I want is to insert selected row data(Grid View) into SQL database. The above code inserts all the data from the gridview to the database. *ExcelToDatatable_dt : Get all the records from the gridview *InsertNewDDIsTemporary_Proc : Insert Procedure *positionList = gets the positions of all the columns of the template database from the SQL server, for instance posList[5] which is company name is a 5th column in SQL database and (posList[5].Length > 0) ? row["CompanyName"].ToString() : "-", will check the row["CompanyName"] from datagridview to be inserted into the company name column in the SQL Server Database.

    Hope you understand the above scenario.
    Any help will be highly appreciated