Get data from SQL along with read excel in C# - c#

I have a program that are able to import an excel file to datatable binding with datagridview. Currently I need to add another column name InvtID and get that InvtID data from sql server based on Barcode column (column '3' as header) that I have been imported.
How am I able to achieve this one as right now my coding are able to get the data AFTER import an excel, so the InvtID column data are not in side-by-side with Barcode column. This is the only problem I encounter to finish this task, please assist me. Thank you
Here how my datatable looks like
public void filldatagridview(ExcelWorksheet workSheet)
{
DataTable dt = new DataTable();
//Create the data column
for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
{
dt.Columns.Add(col.ToString());
}
for (int row = 12; row <= 26; row++)
{
DataRow newRow = dt.NewRow(); //Create a row
int i = 0;
for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
{
newRow[i++] = workSheet.Cells[row, col].Text;
}
dt.Rows.Add(newRow);
}
dt.Columns.RemoveAt(0); //remove No
dt.Columns.RemoveAt(0); //remove article
//Get BookCode
using (SqlConnection conn = new SqlConnection("Server"))
using (SqlCommand cmd = new SqlCommand(null, conn))
{
StringBuilder sb = new StringBuilder("WITH cte AS(SELECT case WHEN InvtID IS NULL OR InvtID='' THEN 'No Bookcode Found' ELSE InvtID END AS InvtID,Barcode,ROW_NUMBER() OVER(PARTITION BY Barcode ORDER BY InvtID Asc) rid FROM InventoryCustomer) SELECT InvtID AS BOOKCODE FROM cte WHERE rid=1 and Barcode In (");
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i != 0) sb.Append(",");
string name = "#P" + i;
cmd.Parameters.AddWithValue(name, dt.Rows[i]["3"]); //"3" is barcode column
sb.Append(name);
}
sb.Append(")");
cmd.CommandText = sb.ToString();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dt.Columns["BOOKCODE"].SetOrdinal(0);
dataGridView2.DataSource = dt;
}
}

I am not sure if this is work.
Avoid adding new row when row is already exists.
Add column first from your dt DataTable
Just take a look at this code:
public void filldatagridview(ExcelWorksheet workSheet)
{
DataTable dt = new DataTable();
DataTable dtInvtID = new DataTable();
dt.Columns.Add("dtInvtID"); //ADDING NEW COLUMN FIRST FOR YOUR dtInvtID
//Create the data column
for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
{
dt.Columns.Add(col.ToString());
}
for (int row = 12; row <= 26; row++)
{
DataRow newRow = dt.NewRow(); //Create a row
int i = 0;
for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
{
newRow[i++] = workSheet.Cells[row, col].Text;
}
dt.Rows.Add(newRow);
}
dt.Columns.RemoveAt(0); //remove No
dt.Columns.RemoveAt(0); //remove article
//Get BookCode
using (SqlConnection conn = new SqlConnection("Server"))
using (SqlCommand cmd = new SqlCommand(null, conn))
{
StringBuilder sb = new StringBuilder("WITH cte AS(SELECT case WHEN InvtID IS NULL OR InvtID='' THEN 'No Bookcode Found' ELSE InvtID END AS InvtID,Barcode,ROW_NUMBER() OVER(PARTITION BY Barcode ORDER BY InvtID Asc) rid FROM InventoryCustomer) SELECT InvtID AS BOOKCODE FROM cte WHERE rid=1 and Barcode In (");
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i != 0) sb.Append(",");
string name = "#P" + i;
cmd.Parameters.AddWithValue(name, dt.Rows[i]["3"]); //"3" is barcode column
sb.Append(name);
}
sb.Append(")");
cmd.CommandText = sb.ToString();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtInvtID);
dt.Columns["BOOKCODE"].SetOrdinal(0);
}
int dtctr = 0;
int ctr = 0;
foreach (DataRow dr in dt.Rows)//inserting the value of your InvtID to dt.Rows
{
dtctr += 1;
ctr = 1;
foreach (DataRow InvtID in dtInvtID.Rows) //Getting the value of dtInvtID from database
{
if (ctr == dtctr)//Condition when the row position is equal (dt.Rows==dtInvtID.Rows) IF THIS NOT RETURN A REAL POSITION THEN YOU CAN RUN IT IN DEBUG MODE T CHECK
{
dr["dtInvtID"] = InvtID[0];
ctr += 1;
break;
}
ctr += 1;
}
}
dataGridView2.DataSource = dt;
}
Reminders:
This code has not tested yet. Any wrong result you can check it and run in Debug Mode.
I've seen some interesting problem:
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i != 0) sb.Append(",");
string name = "#P" + i;
cmd.Parameters.AddWithValue(name, dt.Rows[i]["3"]); //"3" is barcode column
sb.Append(name);
}
sb.Append(")");
Try to Modify it because my answer from above is getting the value of your database. It may cause a redundancy.

you are first inserting new rows into data table with this code.
dt.Rows.Add(newRow);
the second time you are using an adapter to fill the same data table with below code.
da.Fill(dt);
which insert the rows at the end instead of updating the existing rows in the data table.
To align the data into the data table you need to first fill another data table with an adapter then write code using for loop and matching data to update existing or original data table.

Related

How to fetch values from database in pivot while working with dynamic controls in asp.net c#

I'm Prateek, trying to create an application that takes user inputs from the controls that are being generated dynamically such as a TextBox or a DropDownList.
These controls are generated from the database values itself i.e., I'm storing all the values that are to be created in a table named 'usertasks'.
Now, everything is working fine and controls are dynamically generated, the user inputs are stored in another table named 'taskEntries'. The problem I'm facing is with the values being fetched:
The above image is of a table to save what kind of controls the user wants to create and their labels.
Another image to show how data is being stored right now:
How I want the data to be fetched:
Kindly let me know how do I do it in asp.net c#.
What I tried?
//Below code to get all data into a DataTable
protected void getData()
{
using (SqlConnection con = new SqlConnection(ConnectionManager.ConString))
{
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter("select (cdate + ', ' + ctime) as 'Date', taskID as 'Task ID', deptID as 'Department ID', empID as 'Employee ID', question as 'Question', userInput as 'Input' from taskEntries", con))
{
DataTable dt = new DataTable();
sda.Fill(dt);
ViewState["dt"] = dt;
BindGrid(dt, false);
}
con.Close();
}
ConvertRowsToColumns();
}
//Below code to bind the gridview
private void BindGrid(DataTable dt, bool rotate)
{
grdUserData.ShowHeader = !rotate;
grdUserData.DataSource = dt;
grdUserData.DataBind();
if (rotate)
{
foreach (GridViewRow row in grdUserData.Rows)
{
row.Cells[0].CssClass = "header";
}
}
}
//Below code to convert the rows to columns
private void ConvertRowsToColumns()
{
DataTable dt = (DataTable)ViewState["dt"];
DataTable dt2 = new DataTable();
for (int i = 0; i <= dt.Rows.Count; i++)
{
String Question = Convert.ToString(dt.Rows[i]["question"]);
String InputType = Convert.ToString(dt.Rows[i]["inputType"]);
dt2.Columns.Add(Question);
dt2.Columns.Add(InputType);
}
for (int i = 0; i < dt.Columns.Count; i++)
{
dt2.Rows.Add();
dt2.Rows[i][0] = dt.Columns[i].ColumnName;
}
for (int i = 0; i < dt.Columns.Count; i++)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
dt2.Rows[i][j + 1] = dt.Rows[j][i];
}
}
BindGrid(dt2, true);
}

Column data from Sql Server not position as match with column in Datatable

The data in InvtID already match with "3" column once I import an Excel file, but the problem is as shown in the photo why the data column (InvtID) filled in datatable after the last row data? How am I able to fix the position to match with "3" column. I already position the InvtID column at 0. Is there any single line code to start position the row at 1?:
public void filldatagridview(ExcelWorksheet workSheet)
{
DataTable dt = new DataTable();
//Create the data column
for (int col = workSheet.Dimension.Start.Column;
col <= workSheet.Dimension.End.Column; col++)
{
dt.Columns.Add(col.ToString());
}
for (int row = 12; row <= 26; row++)
{
DataRow newRow = dt.NewRow(); //Create a row
int i = 0;
for (int col = workSheet.Dimension.Start.Column;
col <= workSheet.Dimension.End.Column; col++)
{
newRow[i++] = workSheet.Cells[row, col].Text;
}
dt.Rows.Add(newRow);
}
dt.Columns.RemoveAt(0); //remove No
dt.Columns.RemoveAt(0); //remove article
//Get BookCode
var barCodes = dt.AsEnumerable().Select(r => r.Field<string>("3"))
.ToArray(); //EAN column
var barCodesstring = string.Format("'{0}'", string.Join("','", barCodes));
SqlCommand cmd = new SqlCommand("SELECT InvtID FROM InventoryCustomer WHERE
Barcode IN (" + barCodesstring + ")", conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dt.Columns["InvtID"].SetOrdinal(0);
dataGridView2.DataSource = dt;
conn.Close();
}
This code always adds a new row DataRow newRow = dt.NewRow();, so your rows are not matched:
for (int row = 12; row <= 26; row++)
{
DataRow newRow = dt.NewRow(); //Create a row
//...
dt.Rows.Add(newRow);
}
But if I understood correctly, you want to set data into specific data cells. Then you should find existing row and set the cell value:
DataRow dr = dt.Rows[yourRowNumber];
dr[3] = "Your New Value";

Retrieve data from SQL Server based on data column in datatable Epplus C#

My program able to read excel into datatable using epplus C# and there's a barcode column with data. I need to add & get bookcode data column from SQL Server based on data in barcode column. How will the SQL query be able to get each cell data row from the barcode column? I am confused to get the data by column or row.
I managed to get the SQL Server connection and 1 bookcode so far. I think I have to do looping to get all the bookcode for all the barcode column data but I'm not sure with the sequence in my coding and SQL connection. I believe I almost there to get it, hope you guys can assists me as this is my first project in C# :)
public void filldatagridview(ExcelWorksheet workSheet)
{
DataTable dt = new DataTable();
// Create the data column
for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
{
dt.Columns.Add(col.ToString());
}
// for (int row = workSheet.Dimension.Start.Row; row <= workSheet.Dimension.End.Row; row++)
for (int row = 12; row <= 26; row++)
{
DataRow newRow = dt.NewRow(); //Create a row
int i = 0;
for (int col = workSheet.Dimension.Start.Column; col <= workSheet.Dimension.End.Column; col++)
{
newRow[i++] = workSheet.Cells[row, col].Text;
}
dt.Rows.Add(newRow);
}
dt.Columns.RemoveAt(0); //remove No
dt.Columns.RemoveAt(0); //remove article
//Get BookCode
SqlConnection conn = new SqlConnection("Server=10.0.0.10;Database=;UserID=;Trusted_Connection=False");
string query = "SELECT BookCode FROM Inventory WHERE Barcode='" + dt.Rows[1]["3"].ToString().Trim() + "'"; //3 is column for barcode
// looping logic here?
for (int i = 0; i < dt.Rows.Count; i++)
{
}
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView2.DataSource = dt;
conn.Close();
da.Dispose();
}
UPDATED:
Able to get data from sql server based on barcode column in "3" as attached in the following image:
Currently facing problem with the order I receive from database in BOOKCODE column. Where supposedly it should follow like in this image:

novacode docx , create word table from datatable

I'm trying to loop over the datatable and create word table. So far if I have 3 rows in the datatable they are being inserted into the first row of my Microsoft Word table, instead I want every row from the datatable into a new row in Microsoft Word table.
Below is my code :
protected void Button2_Click(object sender, EventArgs e)
{
PullData();
gvd2.DataSource = dataTable;
gvd2.DataBind();
// Create a document.
using (DocX document = DocX.Create(#"D:\Test.docx"))
{
// Add a Table to this document.
Novacode.Table t = document.AddTable(2, 3);
// Specify some properties for this Table.
t.Alignment = Alignment.center;
t.Design = TableDesign.MediumGrid1Accent2;
// Add content to this Table.
t.Rows[0].Cells[0].Paragraphs.First().Append("A");
//foreach (DataRow row in dataTable.Rows)
//{
// t.Rows[1].Cells[0].Paragraphs.First().Append(row["IssueSubjectType"].ToString());
//}
// Loop through the rows in the Table and insert data from the data source.
for (int row = 1; row < t.RowCount; row++)
{
for (int cell = 0; cell < t.Rows[row].Cells.Count; cell++)
{
Paragraph cell_paragraph =t.Rows[row].Cells[cell].Paragraphs[0];
cell_paragraph.InsertText(dataTable.Rows[row - 1].ItemArray[cell].ToString(), false);
}
}
// Insert the Table into the document.
document.InsertTable(t);
// Save the Document.
document.Save();
// Release this document from memory.
document.Dispose();
}
}
private DataTable dataTable = new DataTable();
// method to pull data from database to datatable
public void PullData()
{
using (SqlConnection sqlConn = new SqlConnection("Data Source=.;Initial Catalog=UAE_OG-Interanl;Integrated Security=True"))
{
string sqlQuery = #"SELECT IssueSubjectType from tbl_IssueStoPublicate WHERE IssueNumber = '625' order by IssueSubjectOrder desc";
using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
{
SqlDataAdapter ds = new SqlDataAdapter(cmd);
ds.Fill(dataTable);
}
}
}
Any help would be a lifesaver.
https://github.com/xceedsoftware/DocX/blob/master/Examples/Samples/Table/TableSample.cs
int size = 3;
DocX docX = DocX.Create(result, DocumentTypes.Document);
Table table = docX.AddTable(size, size);
table.AutoFit = AutoFit.Contents;
for (int i = 0; i <= (int)TableBorderType.InsideV; i++)
table.SetBorder((TableBorderType)i, new Border());
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
table.Rows[i].Cells[j].Paragraphs[0].InsertText(i + " | " + j);
docX.InsertParagraph().InsertTableBeforeSelf(table);
docX.Save();

Populate SQL Server data in Excel (2010) spreadsheet

I'm trying to populate the data extracted from SQL Server into Excel 2010. The code below works fine, but the difficulty is that I don't create an Excel spreadsheet programmatically, it is aleady exists and I make a request for data via plugin in Excel written in C#.
Even though I set the cursor to A10 cell, Excel starts filling-out the data from the very first cell and overwrites the header (that is already exists). Please help to fix.
Code:
OdbcConnection cnn;
cnn = new OdbcConnection(azureConn);
using (OdbcCommand command = cnn.CreateCommand())
{
command.CommandText = "{call sp_Get_Excel_Data(?,?,?,?,?,?,?,?)}";
command.Parameters.AddWithValue("#StartDate", startDate);
command.Parameters.AddWithValue("#EndDate", endDate);
command.Parameters.AddWithValue("#startTime", startTime);
command.Parameters.AddWithValue("#endTime", endTime);
command.Parameters.AddWithValue("#smp", smp);
command.Parameters.AddWithValue("#Reg", reg);
command.Parameters.AddWithValue("#event", events);
command.Parameters.AddWithValue("#userId", userId);
cnn.Open();
//DataTable
OdbcDataAdapter adapter = new OdbcDataAdapter(command);
//DataSet
DataSet ds = new DataSet();
adapter.Fill(ds);
//Cast to DataTable
DataTable dataTable = ds.Tables[0];
string[] colNames = new string[dataTable.Columns.Count];
int col = 0;
foreach (DataColumn dc in dataTable.Columns)
colNames[col++] = dc.ColumnName;
w = this.Application.ActiveWorkbook;
ws = (Worksheet)w.ActiveSheet;
Range hdrRow = (Range)ws.Rows[9];
hdrRow.Value = colNames;
hdrRow.Font.Bold = true;
hdrRow.VerticalAlignment = XlVAlign.xlVAlignCenter;
//Position the cursor
var range = ws.get_Range("A10");
range.Select();
//Inserting the Column and Values into Excel file
string data = null;
int i = 0;
int j = 0;
for (i = 0; i <= dataTable.Rows.Count - 1; i++)
{
for (j = 0; j <= dataTable.Columns.Count - 1; j++)
{
data = dataTable.Rows[i].ItemArray[j].ToString();
ws.Cells[i + 2, j + 1] = data;
}
}
Hate to answer my own questions, but here is the solution (with optimized performance):
int column = 1;
foreach (DataColumn c in dataTable.Columns)
{
//Ninth row, starting from the first cell
ws.Cells[10, column] = c.ColumnName;
column++;
}
// Create a 2D array with the data from the data table
int i = 0;
string[,] data = new string[dataTable.Rows.Count, dataTable.Columns.Count];
foreach (DataRow row in dataTable.Rows)
{
int j = 0;
foreach (DataColumn c in dataTable.Columns)
{
data[i, j] = row[c].ToString();
j++;
}
i++;
}
// Set the range value to the 2D array in Excel (10th row, starting from 1st cell)
ws.Range[ws.Cells[11, 1], ws.Cells[dataTable.Rows.Count + 11, dataTable.Columns.Count]].Value = data;

Categories

Resources