How to save multi line values in textbox into multi rows in sql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • huynhtienlinh
    New Member
    • Nov 2012
    • 2

    How to save multi line values in textbox into multi rows in sql

    I'm trying to save multi line values in textbox into multi rows in sql. I have a textbox as shown in below image

    http://www.4shared.com/photo/91DD7knd/Multi-textbox.html

    After save, I want in data sql as below:

    http://www.4shared.com/photo/8yEVRPbV/Data-Sql.html

    Please help me to achieve this in possible, thank you!
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I the method that you have implemented that does the saving, retrieve the string from the multi-line TextBox and split it on New Lines.

    Once you have it split, loop through the elements and create a new string based on 3 elements in the split.

    Then create your insert statements and insert your rows according to your business rules.

    -Frinny

    Comment

    • coding master
      New Member
      • Nov 2012
      • 11

      #3
      huynhtienlinh----- you can try this code

      this code mainly build for excel sheet......

      Code:
       string connectionString = "";
              if (DATAbring.HasFile)
              {
                  string fileName = System.IO.Path.GetFileName(DATAbring.PostedFile.FileName);
                  string fileExtension = System.IO.Path.GetExtension(DATAbring.PostedFile.FileName);
                  string fileLocation = Server.MapPath("~/App_Data/" + fileName);
                  DATAbring.SaveAs(fileLocation);
      
      
                  if (fileExtension == ".xls")
                  {
                      connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                  }
                  else if (fileExtension == ".xlsx")
                  {
                      connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                  }
      
                  OleDbConnection con = new OleDbConnection(connectionString);
                  OleDbCommand cmd = new OleDbCommand();
                  cmd.CommandType = System.Data.CommandType.Text;
                  cmd.Connection = con;
                  OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
                  DataTable dtExcelRecords = new DataTable();
                  con.Open();
                  DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                  string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
                  cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
                  dAdapter.SelectCommand = cmd;
                  dAdapter.Fill(dtExcelRecords);
      
                  int p = dtExcelRecords.Rows.Count;
                  for (int i = 1; i <= p; i++)
                  {
                      string name = Convert.ToString(dtExcelRecords.Rows[i - 1][0]);
                      string mobile = Convert.ToString(dtExcelRecords.Rows[i - 1][1]);
                      con1.Open();
      
                      if (mobile == "")
                      {
                        
                      }
      
                      else
                      {
                          SqlCommand cmmd = new SqlCommand("select C_Name from table_name where Mobile_no='" + mobile.ToString() + "'", con1);
                          int dd = cmmd.ExecuteNonQuery();
                          if (dd > 0)
                          {
      
                          }
                          else
                          {
                              dal d = new dal();
                              SqlCommand cmd1 = new SqlCommand();
                              cmd1 = new SqlCommand("insert into table_name(C_Name,Mobile_no)values('" + name + "','" + mobile + "')", con1);
                              cmd1.ExecuteNonQuery();
                          }
                      }
      
                      con1.Close();
                  }

      Comment

      • huynhtienlinh
        New Member
        • Nov 2012
        • 2

        #4
        Thanks Coding master! I'm trying.

        Comment

        Working...