question

GonzalezLunaEstebanEliud-3790 avatar image
0 Votes"
GonzalezLunaEstebanEliud-3790 asked Viorel-1 answered

Help with a data fill with textbox

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


dotnet-csharpdotnet-aspnet-core-mvc
captura.png (1.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

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();
    }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.