When i am inserting record in Master Child table then exception is raising that
Object reference not set to an instance of an object.
int FileUpload1;
Line 70:
Line 71: foreach (DataRow row in dt.Rows)
Line 72: {
here is my code
DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
dt.Columns.Add("FileUpload1");
dt.Columns.Add("_ID");
this.BindGridview();
}
}
private void BindGridview()
{
DataTable dt = new DataTable();
dt.Columns.Add("RowNumber", typeof(int));
for (int i = 1; i < 6; i++)
{
dt.Rows.Add(i);
}
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
protected void OnUpdate(object sender, EventArgs e)
{
System.IO.Stream fs = FileUpload2.PostedFile.InputStream;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] bytesM = br.ReadBytes((Int32)fs.Length);
using (SqlCommand cmd = new SqlCommand("[DiplayMasterinsert]", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtname.Text);
cmd.Parameters.AddWithValue("@Data", bytesM);
con.Open();
_ID = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
DataTable dt = (DataTable)ViewState["dt"];
int FileUpload1;
foreach (DataRow row in dt.Rows)
{
//pid = int.Parse(row["_PID"].ToString());
FileUpload1 = int.Parse(row["FileUpload1"].ToString());
this.InsertRows(FileUpload1);
}
}
}
private void InsertRows(int data)
{
con.Open();
using (SqlCommand cmd = new SqlCommand("[Insert_DisplayDetail]", con))
{
foreach (GridViewRow row in Gridview1.Rows)
{
FileUpload upload = (FileUpload)row.FindControl("FileUpload1");
// if (!string.IsNullOrEmpty(customerName) && !string.IsNullOrEmpty(country) && !string.IsNullOrEmpty(upload.PostedFile.FileName))
{
System.IO.Stream fs = upload.PostedFile.InputStream;
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", _ID);
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
}
public int _ID { get; set; }