Transforming an array into a string

After successfully creating a function to work with tags, I encountered an issue where the data is stored in the database as an array instead of a string. How can I resolve this problem?

  private tags: string[] = [];

private addTagToProduct($event): void {
    this.tags.push($event.target.value);
    $event.target.value = "";
  }

  private deleteTagFromProduct(tag: string): void {
    this.tags = this.tags.filter((a) => a !== tag);
  }

SERVER

string tags;

public string Tags { get => tags; set => tags = value; }

  public Rep CreateProduct(SqlConnection conn, Product products)
        {
            SqlCommand cmd = conn.CreateCommand();
            SqlTransaction transaction;
            conn.Open();
            transaction = conn.BeginTransaction();

            try
            {
                cmd.CommandText = "INSERT INTO[Products](Tags)" +
                            " Values (@TagS)";
                        cmd.Parameters.Clear();
                        cmd.Parameters.Add("@Tags", SqlDbType.VarChar).Value = Tags;
                        cmd.Transaction = transaction;

                        cmd.ExecuteNonQuery();
                        transaction.Commit();          
            }

            catch (Exception ex)
            {
                transaction.Rollback();                
            }
            finally
            {
                conn.Close();
            }
        }

Answer №1

To transmit a text to the server, simply execute this line of code:

this.items.map((text) => "'" + text + "'").join(",")

Afterwards, transmit it to your server. It is crucial to guard against SQL injection by utilizing an array and crafting your insertion method accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Customizing the appearance of Previous and Next elements using JavaScript

My challenge involves working with a form on a webpage where I do not have direct access to the HTML or CSS. Therefore, any modifications need to be made using JavaScript\JQuery. The form in question has a layout like this: https://i.sstatic.net/Czuo ...

Angular - Error caused by a change in an expression after it has been checked

Check out my StackBlitz project here - https://stackblitz.com/edit/svg-donuts-fhnnmo?file=src%2Fapp%2Fdonuts.component.ts I have implemented three donut charts using SVG stroke-dasharray The position of each chart segment depends on the previous segment& ...

Utilize Selenium's explicit wait feature to wait until a group of buttons on a web page become clickable

On a webpage, there is a collection of buttons. Each button has the same source code except for the link text. I am looking to verify that all the buttons on the page are clickable using WebDriverWait.until. Currently, I can confirm the clickability of th ...

Node.js server experiencing delays due to V8 processing constraints

REVISED I am currently running a nodeJS http server designed to handle uploads from multiple clients and process them separately. However, I have encountered an issue where the first request seems to block any subsequent requests until the first one is co ...

Storing pictures in MongoDB as binary large objects using React

I have been working on utilizing the dropzone-react component for uploading images. Upon successful upload, it provides me with the blob:http://test address which allows me to view the uploaded image. However, I am facing a challenge in saving this image a ...

Angular 8: Issue with PatchValue in Conjunction with ChangeDetector and UpdateValue

I am puzzled by the fact that PatchValue does not seem to work properly with FormBuilder. While it shows data when retrieving the value, it fails to set it in the FormBuilder. Does anyone have an idea why this might be happening? I am utilizing UpdateValue ...

Error: The object does not have the property createContext necessary to call React.createContext

I'm currently exploring the new React Context API in my app. I've also implemented flow for type checking. However, when I add // @flow to a file that contains the code: const MyContext = React.createContext() An error pops up stating: Cannot ...

The outer DIV will envelop and grow taller in conjunction with the inner DIV

Could use a little help here. Thank you :) I'm having trouble figuring out how to get the outer div to wrap around the inner div and expand upwards with the content inside the inner editable div. The inner div should expand from bottom to top, and t ...

Tips for creating a for loop in a .js script within MongoDB that allows for passing a variable containing the database name to a text file

In a .txt file, I have a list of database names as shown below: local test admin Is there a way to dynamically pass arguments instead of hardcoding them in .js scripts for mono go? db = db.getSiblingDB('test'); date = new Date() dat ...

How come the location.replace() function is called even as an AJAX request is in progress?

Below is the code snippet that I'm working with: getClients(id); location.replace('/login/index.html'); The getClients() function serves as an asynchronous AJAX request. If the list of clients is empty, the page gets redirected to another ...

Learn the best way to populate Google Map popup windows with multiple values and include a button to pass unique ID values

I'm facing an issue with my code. When I click on a marker, it should display "Madivala, 12.914494, 77.560381,car,as12" along with a button to pass ID values. Can someone help me figure out how to move forward? http://jsfiddle.net/cLADs/123/ <ht ...

Enhancing a React modal to enable user input for updating state variables

Is there a way to utilize user input from the page to dynamically create elements in a React.js application? In my app.js file, I have defined some constants at the beginning for mock data: const book1 = [ {id: uuid(), content: 'reflections&apos ...

JQuery Form Submission Failing to Trigger Controller Function

Attempting to submit a form in a JSP using JQuery/AJAX to call a method in a Spring Controller. The JSP structure is as follows: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> < ...

What are the best techniques for creating animations in AngularJS?

I've been trying to figure out how to animate in AngularJS by searching online, but I haven't found a perfect solution yet. html <span class="sb-arrow down" ng-click="hideSampleList($event)"></span> ...

Is it possible to run two commands in npm scripts when the first command initiates a server?

When running npm scripts, I encountered an issue where the first command successfully starts a node server but prevents the execution of the second command. How can I ensure that both commands are executed successfully? package.json "scripts": { "dev ...

Dynamic PHP content manipulated with jQuery Add/Remove functionality

I am new to jQuery and currently working on implementing a feature for my PHP page. I want to have an Add/Remove button/icon that, when clicked, will show a new set of fields (specifically Drop Down menus). The example I found only shows how to add a text ...

How does the JavaScript event handler affect the value of my PHP variable?

I have a webpage called page1.php that has a form posting to another PHP page named page2.php which contains an access login form. I am attempting to automatically insert 2 of the $_POST variables into the access login form. The process on page2.php goes l ...

What could be the reason my span is not altering color as I scroll?

Important HTML Knowledge <section class="home" id="home"> <div class="max-width"> <div class="home-content"> <div class="text-1">Hey t ...

Troubleshoot: Bootstrap Tooltips visible in DOM but not functioning

I am currently attempting to utilize bootstrap's tooltip feature on a dynamically created set of divs. When hovering, I can see in Chrome's inspector that the div itself is being modified correctly (title value is passed to data-original-title) a ...

I am having trouble toggling radio buttons in React

class VoiceSelector extends Component { constructor(props){ this.handleCheck = this.handleCheck.bind(this); } state={ voices : [ {"Voice":"Indian English Female Voice 1"}, {"Voice":&qu ...