Hi asifip,
I have created sample that full fill your requirement. To get the record i used the below table.
Sql
CREATE TABLE FruitTable
(
Id int NULL,
Fruit VARCHAR(50) NULL,
Price NUMERIC(18,2) NULL
)
INSERT FruitTable (Id, Fruit, Price) VALUES (1, N'Apple', '3.2')
INSERT FruitTable (Id, Fruit, Price) VALUES (2, N'Orange', '4')
INSERT FruitTable (Id, Fruit, Price) VALUES (3, N'Melon', '3')
INSERT FruitTable (Id, Fruit, Price) VALUES (4, N'Cherry', '5')
INSERT FruitTable (Id, Fruit, Price) VALUES (5, N'Grapes', '2')
HTML
CS.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Search" ID="btnSearch" runat="server" OnClick="Search" />
<br />
<asp:TextBox ID="txtCopied" runat="server" TextMode="MultiLine" AutoPostBack="true"
OnTextChanged="PasteToGridView" Height="200" Width="400" />
<script type="text/javascript">
window.onload = function () {
document.getElementById("<%=txtCopied.ClientID %>").onpaste = function () {
var txt = this;
setTimeout(function () { __doPostBack(txt.name, ''); }, 100);
}
};
</script>
Default.aspx
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product Name" />
<asp:BoundField DataField="Price" HeaderText="Price" DataFormatString="{0:C}" />
</Columns>
</asp:GridView>
</div>
Code
CS.aspx.cs
protected void PasteToGridView(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("ProductName") });
string copiedContent = Request.Form[txtCopied.UniqueID];
foreach (string row in copiedContent.Split('\n'))
{
if (!string.IsNullOrEmpty(row))
{
dt.Rows.Add();
int i = 0;
foreach (string cell in row.Split('\t'))
{
dt.Rows[dt.Rows.Count - 1][i] = cell;
i++;
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
Session["PastedData"] = dt;
txtCopied.Text = "";
}
protected void Search(object sender, EventArgs e)
{
if (Session["PastedData"] != null)
{
Response.Redirect("Default.aspx", false);
}
}
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["PastedData"] != null)
{
DataTable dt = Session["PastedData"] as DataTable;
string productName = "";
foreach (DataRow dr in dt.Rows)
{
productName += "'" + dr[0].ToString().Trim().Replace("\r", "") + "',";
}
string query = "SELECT Fruit ProductName,Price FROM FruitTable WHERE Fruit IN (" + productName.Remove(productName.Length - 1) + ")";
GridView1.DataSource = GetProductWithPrice(query);
GridView1.DataBind();
}
}
private DataTable GetProductWithPrice(string query)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = ConfigurationManager.ConnectionStrings[1].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
con.Open();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
sda.Fill(dt);
}
con.Close();
}
}
return dt;
}
Screenshot