Skip to content

Commit

Permalink
DnaExtract Batch Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinkobit committed Jul 18, 2023
1 parent ed72bc2 commit 7de7817
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 6 deletions.
161 changes: 160 additions & 1 deletion Labtracker/Controllers/DnaExtractExcelController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System;
using ExcelDataReader;
using Labtracker.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
Expand All @@ -11,7 +16,161 @@ public class DnaExtractExcelController : Controller
// GET: DnaExtractExcel
public ActionResult Index()
{
DataTable dt = new DataTable();
try
{
dt = (DataTable)Session["tmpdata"];
}
catch (Exception)
{
throw;
}

return View(dt);
}


[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(HttpPostedFileBase upload)
{

if (ModelState.IsValid)
{

if (upload != null && upload.ContentLength > 0)
{
// ExcelDataReader works with the binary Excel file, so it needs a FileStream
// to get started. This is how we avoid dependencies on ACE or Interop:
Stream stream = upload.InputStream;

// We return the interface, so that
IExcelDataReader reader = null;


if (upload.FileName.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (upload.FileName.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
else
{
ModelState.AddModelError("File", "This file format is not supported");
return View();
}
int fieldcount = reader.FieldCount;
int rowcount = reader.RowCount;


DataTable dt = new DataTable();
//dt.Columns.Add("UserName");
//dt.Columns.Add("Adddress");
DataRow row;


DataTable dt_ = new DataTable();
string err1 = "", err2 = "";
try
{

dt_ = reader.AsDataSet().Tables[0];

string ret = "";

for (int i = 0; i < dt_.Columns.Count; i++)
{
dt.Columns.Add(dt_.Rows[0][i].ToString());
}

int rowcounter = 0;
for (int row_ = 1; row_ < dt_.Rows.Count; row_++)
{
row = dt.NewRow();

for (int col = 0; col < dt_.Columns.Count; col++)
{

row[col] = dt_.Rows[row_][col].ToString();
rowcounter++;
}
dt.Rows.Add(row);

err1 = row[9].ToString();
err2 = row[10].ToString();
//db insert
try
{
using (var context = new SampleContext())

{



var tbgdnaextractupdates = new Dnaextract();

tbgdnaextractupdates.PatientId = row[0].ToString();
tbgdnaextractupdates.TubeLabel = row[1].ToString();
tbgdnaextractupdates.GoodQQ = row[2].ToString();
tbgdnaextractupdates.NDConc = string.IsNullOrWhiteSpace(row[3].ToString()) ? (decimal?)null : Convert.ToDecimal(row[3].ToString());
tbgdnaextractupdates.Purity = string.IsNullOrWhiteSpace(row[5].ToString()) ? (decimal?)null : Convert.ToDecimal(row[5].ToString());
tbgdnaextractupdates.SampleType = row[6].ToString();
tbgdnaextractupdates.QubitConc = row[7].ToString();

tbgdnaextractupdates.AssayReagent = row[9].ToString();
tbgdnaextractupdates.ExtractDate = string.IsNullOrEmpty(row[10].ToString()) ? (DateTime?)null : Convert.ToDateTime(row[10].ToString());

//tbgdnaextractupdates.Initial = labInitial;
//tbgdnaextractupdates.Remark = remark;

context.Dnaextracts.Add(tbgdnaextractupdates);
context.SaveChanges();


}
}
catch (Exception ex1)
{
throw ex1;

}

}

}
catch (Exception ex)
{

ModelState.AddModelError("File", ex.ToString() + err1 + err2 + "Unable to Upload file! Info: Data Validation Error, Invalid data entry.");

return View();
}

DataSet result = new DataSet();//reader.AsDataSet();
result.Tables.Add(dt);
string minutes_ID = "";

reader.Close();
reader.Dispose();
// return View();
// return View(result.Tables[0]);

DataTable ddd = result.Tables[0];

Session["tmpdata"] = ddd;

return RedirectToAction("Index");

}
else
{
ModelState.AddModelError("File", "Please Choose Your file to Upload");
}
}
return View();
}

}
}
116 changes: 114 additions & 2 deletions Labtracker/Views/DnaExtractExcel/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,119 @@


@{
ViewBag.Title = "Index";
ViewBag.Title = "DNA Extract Excel Upload";
}

@model System.Data.DataTable
@using System.Data;




<script src="~/Scripts/jquery-3.3.1.js"></script>

<script>
function bs_input_file() {
$(".input-file").before(
function () {
if (!$(this).prev().hasClass('input-ghost')) {
var element = $("<input type='file' id='dataFile' name='upload' class='input-ghost' style='visibility:visible; height:20;'>");
element.attr("name", $(this).attr("name"));
element.change(function () {
element.next(element).find('input').val((element.val()).split('\\').pop());
});
$(this).find("button.btn-choose").click(function () {
element.click();
});
$(this).find("button.btn-reset").click(function () {
element.val(null);
$(this).parents(".input-file").find('input').val('');
});
$(this).find('input').css("cursor", "pointer");
$(this).find('input').mousedown(function () {
$(this).parents('.input-file').prev().click();
return false;
});
return element;
}
}
);
}
function clear() {
var input = $("#dataFile").val('');
};
$(function () {
clear();
bs_input_file();
});
</script>

@using (Html.BeginForm("Index", "DnaExtractExcel", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="info2">
<h2>DnaExtract Excel File Upload</h2>
@Html.AntiForgeryToken()

<div class="form-group" name="Fichier1">
<!-- COMPONENT START -->
<div class="form-group">
<div class="input-group input-file">
<span class="input-group-btn">
@*<button class="btn btn-default btn-choose" type="button">Choose</button>*@
</span>
@*<input type="text" class="form-control" placeholder='Choose a file...' />*@

</div>
</div>
@*<input type="file" id="dataFile" name="upload"/>*@

</div>

<div class="form-group">
<input type="submit" value="Import from Excel" class="d-none d-sm-inline-block btn btn-sm btn-primary shadow-sm" />
@*<button class="btn btn-default" style="background-color:Highlight" >Save</button>*@
</div>


<div class="alert alert-danger">
@Html.ValidationSummary()
@*<strong></strong> asdasd*@
</div>



</div>
if (Model != null)
{
<table id="table1" class="table table-bordered table-hover">
<thead>
<tr>
@foreach (DataColumn col in Model.Columns)
{
<th>@col.ColumnName</th>
}
</tr>
</thead>
<tbody>
@foreach (DataRow row in Model.Rows)
{
<tr>
@foreach (DataColumn col in Model.Columns)
{
<td>@row[col.ColumnName]</td>
}
</tr>
}
</tbody>
</table>
}
}

<h2>Index</h2>
<script>
$(document).ready(function () {
$("table1").DataTable();
});
</script>


6 changes: 4 additions & 2 deletions Labtracker/sequencing.aspx
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,14 @@
<!-- Area Chart -->
<div class="col-xl-12 col-lg-8">
<div class="card shadow mb-4 table-responsive">
<p>DNA Extraction: <asp:Label runat="server" ID="lbldnaprocessed" Text=""></asp:Label></p>
<p>DNA Extraction: <asp:Label runat="server" ID="lbldnasall"></asp:Label> extracts, <asp:Label runat="server" ID="lbldnaprocessed" Text=""></asp:Label> unique IDs</p>
<asp:GridView ID="gvResult" runat="server" style="font-size:12px" width="1200px" CellPadding="3" AutoGenerateColumns="False" DataKeyNames="PatientId" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" OnDataBound="gvResult_DataBound" >
<Columns>
<asp:CommandField ShowSelectButton="True" />
<%--<asp:BoundField DataField="ResultID" HeaderText="ResultID" InsertVisible="False" ReadOnly="True" SortExpression="ResultID" />--%>
<asp:BoundField DataField="PatientId" HeaderText="PatientId" SortExpression="PatientId" />
<asp:BoundField DataField="TubeLabel" HeaderText="TubeLabel" SortExpression="TubeLabel" />
<asp:BoundField DataField="GoodQQ" HeaderText="GoodQQ" SortExpression="GoodQQ" />
<asp:BoundField DataField="NDConc" HeaderText="NDConc" SortExpression="NDConc" />
<asp:BoundField DataField="QubitConc" HeaderText="QubitConc" SortExpression="QubitConc" />

Expand All @@ -370,7 +372,7 @@
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Labtracker %>" SelectCommand="SELECT [PatientId],[NDConc],[QubitConc],[Purity],[ExtractDate],[Initial],[Remark] FROM Dnaextracts"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Labtracker %>" SelectCommand="SELECT [PatientId], [TubeLabel],[GoodQQ],[NDConc],[QubitConc],[Purity],[ExtractDate],[Initial],[Remark] FROM Dnaextracts"></asp:SqlDataSource>

</div>
</div>
Expand Down
9 changes: 8 additions & 1 deletion Labtracker/sequencing.aspx.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNet.Identity;
using Labtracker.Models;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Configuration;
Expand All @@ -21,12 +22,18 @@ protected void Page_Load(object sender, EventArgs e)
}
if (!IsPostBack)
{
using (SampleContext _db= new SampleContext())
{
lbldnasall.Text = _db.Dnaextracts.Count().ToString();
}

string connStr = ConfigurationManager.ConnectionStrings["Labtracker"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
string sql = "SELECT COUNT(DISTINCT PatientId) FROM Dnaextracts";
string sql2 = "SELECT COUNT(DISTINCT PatientId) FROM LibraryPreps";



using (SqlCommand cmd = new SqlCommand(sql, conn))
Expand Down
9 changes: 9 additions & 0 deletions Labtracker/sequencing.aspx.designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7de7817

Please sign in to comment.