I have two table ContCatagory and second one is Containerno
I am using below link to copy data from Excel file and paste to gridview as define in Link Below.
CREATE TABLE [dbo].[ConCatagory](
[Cat_ID] [int] IDENTITY(1,1) NOT NULL,
[Cat_Name] [varchar](50) NULL,
)
INSERT INTO ConCatagory VALUES(1,'AA')
INSERT INTO ConCatagory VALUES(1,'BB')
Create TABLE Containerno(
CID int,
Contno varchar(50),
ConWeight int,
No_Of_Bales int,
Cat_Id int,
entrydate date
)
Below data which i will copy and paste
ID |
Barcode |
Weight |
Qty |
Category |
ContNo |
1 |
AKHTER0002 |
5000 |
45 |
BB |
XYZJKNF121 |
2 |
AKHTER0003 |
5000 |
45 |
BB |
XYZJKNF121 |
Issue is that User will Copy Cat_Name 'BB', so how i will get Cat_ID from Cat_Name before inserting record into database table containerno.
HTML
<script type="text/javascript">
window.onload = function () {
document.getElementById("<%=txtCopied.ClientID %>").onpaste = function () {
var txt = this;
setTimeout(function () {
__doPostBack(txt.name, '');
}, 100);
}
};
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="ContNo" HeaderText="Barcode" ItemStyle-Width="150" />
<asp:BoundField DataField="ConWeight" HeaderText="Weight" ItemStyle-Width="150" />
<asp:BoundField DataField="No_Of_Bales" HeaderText="Qty" ItemStyle-Width="150" />
<asp:BoundField DataField="Cat_ID" HeaderText="Category" ItemStyle-Width="150" />
<asp:BoundField DataField="Contrackno" HeaderText="Cont_No" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click1" />
<br />
<asp:TextBox ID="txtCopied" runat="server" TextMode="MultiLine" AutoPostBack="true"
OnTextChanged="PasteToGridView" Height="200" Width="400" /></td>
C#
protected void PasteToGridView(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] {
new DataColumn("Id", typeof(int)),
new DataColumn("ContNo", typeof(string)),
new DataColumn("ConWeight",typeof(string)),
new DataColumn("No_Of_Bales",typeof(string)),
new DataColumn("Cat_ID",typeof(string)),
new DataColumn("Contrackno",typeof(string)),
});
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();
ViewState["dt"] = dt;
txtCopied.Text = "";
}
protected void Button1_Click1(object sender, EventArgs e)
{
DataTable dt = (DataTable)ViewState["dt"];
string barcode, qty, weight, cat_id, contno;
foreach (DataRow row in dt.Rows)
{
barcode = (row["ContNo"].ToString());
weight = (row["ConWeight"].ToString());
qty = (row["No_Of_Bales"].ToString());
cat_id = (row["Cat_ID"].ToString());
contno = (row["Contrackno"].ToString());
this.InsertRows(barcode, qty, weight, cat_id, contno);
}
}
private void InsertRows(string barcode, string qty, string weight, string cat_id, string contno)
{ //con.Open();
SqlCommand cmd = new SqlCommand("Sp_Insert_ContnoM", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Action", "Insert");
cmd.Parameters.AddWithValue("@ContNo", barcode);
cmd.Parameters.AddWithValue("@ConWeight", weight);
cmd.Parameters.AddWithValue("@No_Of_Bales", qty);
cmd.Parameters.AddWithValue("@Contrackno", contno);
cmd.Parameters.AddWithValue("@Cat_Id", cat_id);
cmd.Parameters.AddWithValue("@entrydate", DateTime.Now);
con.Open();
cmd.ExecuteNonQuery();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script>alert('Record save successuly')</script>");
}
else
{
Response.Write("<script>alert('Record Not save')</script>");
}
con.Close();
}