0
votes

On an asp.net web form, I have: multiline TextBox button GridView

The TextBox is the entry for PRODUCT_ID. When PRODUCT_ID is entered in TextBox and button clicked, it will return rows from the Product table and display in GridView.

I created DataSet with TableAdapter. Here's the query for the Get method:

SELECT        PRODUCT_ID, PRODUCT_NAME
FROM            TEST.PRODUCT
WHERE        (PRODUCT_ID = ?)

Here's the code:

protected void Button2_Click(object sender, EventArgs e) { short id = short.Parse(TextBox2.Text);

    MyDataSetTableAdapters.PRODUCTTableAdapter idDS = new PRODUCTTableAdapter();

    MyDataSet.PRODUCTDataTable idDT = idDS.GetID(id);

    GridView2.DataSource = idDT;
    GridView2.DataBind();
}

It works when I only enter one PRODUCT_ID in the TextBox. For example,

if I enter "8", it will display row from PRODUCT table where PRODUCT_ID = 8;
if I enter "9", it will display row from PRODUCT table where PRODUCT_ID = 9.

The idea is to enter PRODUCT_ID in the TextBox (one or multiple) and display matching rows in GridView.

However, when I tried to enter more than ID in TextBox, for example: 8 9 (then click the button)

It did not work.

Error message says - {"Input string was not in a correct format."}

How do I pass input from multiline TextBox to TableAdapter query?

1

1 Answers

0
votes

You need to change your code design a bit.

First, change your method GetID to accept a string.

 public void GetID(string ids)
 {
   //your code
 }

Second, change your multiLine Textbox input to comma separated string.

  string ids= string.Join(",", TextBox1.Text.Split(' '));

Third, change your SQL query where clause.

SELECT        PRODUCT_ID, PRODUCT_NAME
FROM            TEST.PRODUCT
WHERE        (PRODUCT_ID in (ids))

Hope this helps.