I am trying to insert master detail data into table, master data is inserting into table but detail data from gridview is not getting insert into table (i am import data from excel file into gridview ).
below is my code
namespace WebApplication1
{
public partial class Import : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=SSS;MultipleActiveResultSets=True;");
// public object Orderno { get; private set; }
DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt.Columns.Add("Descriptionitem");
dt.Columns.Add("Codeitem");
dt.Columns.Add("orderqty");
ViewState["dt"] = dt;
loadcustomer();
txtdate.Text = DateTime.Now.ToString();
}
}
private void loadcustomer()
{
con.Open();
SqlDataAdapter adpbp = new SqlDataAdapter("select * from Customer ", con);
DataSet dsbp = new DataSet();
adpbp.Fill(dsbp);
DDLCus.DataSource = dsbp.Tables[0];
DDLCus.DataTextField = "CustomerName";
DDLCus.DataValueField = "CustomerID";
DDLCus.DataBind();
DDLCus.Items.Insert(0, new ListItem("Select Item Name", ""));
con.Close();
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string path = Path.GetFileName(FileUpload1.FileName);
path = path.Replace(" ", "");
FileUpload1.SaveAs(Server.MapPath("~/ExcelFile/") + path);
String ExcelPath = Server.MapPath("~/ExcelFile/") + path;
OleDbConnection mycon = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + ExcelPath + "; Extended Properties=Excel 8.0; Persist Security Info = False");
mycon.Open();
OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", mycon);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
mycon.Close();
Label3.Text = "Excel File Has Been Saved and Data Captured";
}
protected void btn_add_Click(object sender, EventArgs e)
{
using (SqlCommand cmd = new SqlCommand("Sp_OrderDetail_CRUD", con))
{
cmd.Parameters.AddWithValue("@Action", "INSERTM");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Order_Ref_No", txtRefno.Text);
cmd.Parameters.AddWithValue("@CustomerID", DDLCus.SelectedValue);
cmd.Parameters.AddWithValue("@Status", DDLStatus.SelectedValue);
cmd.Parameters.AddWithValue("@SOpirority", txtPiority.Text);
cmd.Parameters.AddWithValue("@Order_date", txtdate.Text);
con.Open();
Orderno = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
DataTable dt = (DataTable)ViewState["dt"];
int codeitem, orderqty;
foreach (DataRow row in dt.Rows)
{
orderqty = int.Parse(row["orderqty"].ToString());
codeitem = int.Parse(row["Codeitem"].ToString());
this.InsertRows(codeitem, orderqty);
}
}
}
private void InsertRows(int codeitem, int orderqty)
{
//con.Open();
using (SqlCommand cmd = new SqlCommand("Sp_OrderDetail_CRUD", con))
{
cmd.Parameters.AddWithValue("@Action", "INSERT");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Orderno", Orderno);
cmd.Parameters.AddWithValue("@CodeItem", codeitem);
cmd.Parameters.AddWithValue("@orderqty", orderqty);
con.Open();
cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('Duplicate Order Save SuccessFully.'); window.open('SOgvud.aspx');", true);
con.Close();
}
}
public object Orderno { get; set; }
}
}
S.P
Create procedure [dbo].[Sp_OrderDetail_CRUD]
@Action VARCHAR(10)
,@Codeitem INT = NULL
,@OrderNO INT = NULL
,@orderqty INT = Null,
@Order_Ref_No Varchar(50) = null,
@CustomerID INT = null,
@Order_date date = null,
@SOpirority int = null,
@Status Varchar(50)= null
AS
BEGIN
SET NOCOUNT ON;
--INSERT
IF @Action = 'INSERT'
BEGIN
INSERT INTO OrderDetail (Orderno,Codeitem,orderqty) Values(@Orderno,@Codeitem,@orderqty)
END
--INSERT
IF @Action = 'INSERTM'
BEGIN
INSERT INTO SalesOrder (Order_Ref_No,CustomerID,Order_date,SOpirority,Status) Values(@Order_Ref_No,@CustomerID,@Order_date,@SOpirority,@Status)
SELECT SCOPE_IDENTITY()
END
end