Help with a data fill with textbox

EvansGxz 121 Reputation points
2021-06-29T16:02:37.27+00:00

Hello, I want to add multiple pieces of data to the database with only one textbox.
I have a form with SQL Connection, I'm doing a program that upload employees to a site or plant, my idea it's fill with 1 form and in the textbox add more of one employee, here is an example to how should it work if i write employee id.
Example:

employee id (Textbox): 9991, 9992, 8873
Site: US

So when I click on Accept_Button it should save the data in database like this:
110374-captura.png

Here it's the code I have
HTML:

<div class="item">  
   <label for="Id">ID<span>*</span></label>  
   <input id="Id" type="text" runat="server" required="required"/>   
`</div>  
 <div class="item">  
    <label for="Site">SITE<span>*</span></label>  
    <input id="Site" type="text" runat="server" required="required"/>  
 </div>  

C#:

using (SqlConnection sqlCon = new SqlConnection(connectionString))  
{  
     sqlCon.Open();  
     string query = "INSERT INTO Employees (EID, Site) VALUES (@EID,@EID)";  
            SqlCommand sqlCmd = new SqlCommand(query, sqlCon);  
            sqlCmd.Parameters.AddWithValue("@EID", Id.Value);  
            sqlCmd.Parameters.AddWithValue("@Site", Site.Value);  
            sqlCmd.ExecuteNonQuery();  
}  

I try to use a Split function but I don't know how to add it in my program.
I have this but I don't know how add it or use it:

    var sep = new string[] { ", " };  
    string[] subs = Id.Value.Split(sep, StringSplitOptions.None);  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,140 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,204 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 111.8K Reputation points
    2021-06-29T16:59:07.363+00:00

    Try something like this:

    using (SqlConnection sqlCon = new SqlConnection(connectionString))
    {
       sqlCon.Open();
       string query = "INSERT INTO Employees (EID, Site) VALUES (@EID, @Site)";
       SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
       foreach( var id in Id.Value.Split(','))
       {
          sqlCmd.Parameters.Clear();
          sqlCmd.Parameters.AddWithValue("@EID", id.Trim());
          sqlCmd.Parameters.AddWithValue("@Site", Site.Value.Trim());
          sqlCmd.ExecuteNonQuery();
       }
    }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful