Turn csv stream parser sql insert into bulk insert - c#
I have this specific way of getting a CSV where a web service generates the CSV file in the web browser and I then grab all of the data and parse it and stream it into variables and do an SQL insert for each row. Now the problem is this takes to long and I am not sure how to convert this to do a bulk insert.
my code is below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.FileIO;
using System.IO;
using System.Data.SqlClient;
using System.Data.Sql;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
WebClient client = new WebClient();
Uri ur = new Uri("http://1.1.1.1/portal/FEDE?ORGANIZATION_ID=96&SERVLET_ACTION=getItemsList");
public void scrapeCSV()
{
Stream myStream = client.OpenRead(ur);
StreamReader stream = new StreamReader(myStream);
//Parse the stream
using (TextFieldParser parser = new TextFieldParser(stream))
{
parser.TextFieldType = FieldType.Delimited;
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
int rowCount = 1, colCount = 1;
string strInsert = "";
string rowName = "";
string itemCode = "", description = "", barcode = "";
int boxQty = 0, palletQty = 0;
double weight = 0.0;
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["casi"].ConnectionString);
conn.Open();
SqlCommand cmd1 = new SqlCommand("delete from itemslist", conn);
cmd1.ExecuteNonQuery();
while (!parser.EndOfData)
{
//Processing row
string[] row = parser.ReadFields();
rowName = row[0].ToString();
if (rowCount > 2) //Skip header row
{
foreach (string field in row)
{
if (colCount == 1)
{
itemCode = field;
}
else if (colCount == 2)
{
description = field.Replace("'", "''"); ;
}
else if (colCount == 3)
{
if (field != "")
{
boxQty = Convert.ToInt32(field);
}
else
{
boxQty = 0;
}
}
else if (colCount == 4)
{
if (field != "")
{
palletQty = Convert.ToInt32(field);
}
else
{
palletQty = 0;
}
}
else if (colCount == 5)
{
if (field != "")
{
weight = Convert.ToDouble(field);
}
else
{
weight = 0.0;
}
}
else if (colCount == 6)
{
barcode = field;
}
colCount++;
}
colCount = 1;
strInsert = #"INSERT INTO ItemsList (ItemCode, Description, BoxQty, PalletQty,Weight,Barcode)
VALUES ('" + itemCode + "', '" + description + "', '" + boxQty + "', '" + palletQty + "', '" + weight + "', '" + barcode + "')";
SqlCommand cmd2 = new SqlCommand(strInsert, conn);
try
{
cmd2.ExecuteNonQuery();
}
catch (Exception ex)
{
//put code trace log here such as write a txt file content ex.tostring;
if (ex is FormatException || ex is OverflowException)
{
Response.Write(strInsert);
continue;
}
//continue;//will run code bellow cmd.ExecuteNonQuery(); or you can put any code running if
Response.Write(strInsert);
continue;
}
}
this.Label1.Text = Convert.ToString(rowCount);
rowCount++;
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
scrapeCSV();
Response.Write("Download finished!");
}
}
If someone is able to help me figure this out that would be very appreciated.
You need to create a DataTable and then add a mapping to the datatable so the columns of the DataTable are mapped to the columns of the database. I got you started by creating and filling the datatable. Now search web for info on bulk copy datatable to database.
DataTable dt = new DataTable();
dt.Columns.Add("ItemCode", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("BoxQty", typeof(int));
dt.Columns.Add("PalletQty", typeof(int));
dt.Columns.Add("Weight", typeof(decimal));
dt.Columns.Add("Barcode", typeof(string));
//inside while loop
dt.Rows.Add(new object[] {itemCode, description, boxQty, palletQty, weight, barcode});
Related
Select Multiple CSV and bulk to Database
I'm using an MDF database with the OpenFileDialog class to import a single CSV file to a database. That works fine for a single CSV file, but I need to open and process multiple CSV files in bulk. How can I improve my code? I've tried using a for loop. Here is my code to process a single CSV file: ofd.Filter = "CSV files (*.csv) | *.csv; | CSV PRN files (*.prn,) |*.prn;"; ofd.FileName = ""; ofd.ShowDialog(); DataTable dt = new DataTable(); string line = null; int i = 0; using (StreamReader sr = File.OpenText(ofd.FileName)) { while ((line = sr.ReadLine()) != null) { string[] data = line.Split(','); if (data.Length > 0) { if (i == 0) { foreach (var item in data) { dt.Columns.Add(new DataColumn()); } i++; } DataRow row = dt.NewRow(); row.ItemArray = data; dt.Rows.Add(row); } } } string symbolName = dt.Rows[1][0].ToString(); string strConnection = #"Data Source =.\SQLEXPRESS; AttachDbFilename = C:\USERS\JEF\DOCUMENTS\DATABASE1.MDF; Integrated Security = True; Connect Timeout = 30; User Instance = True"; SqlConnection condb2 = new SqlConnection(strConnection); string createTablerow ="create table ["+symbolName+"] (code1 VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,date1 varchar(50),open1 varchar(50),high1 varchar(50),low1 varchar(50),close1 varchar(50),vol1 varchar(50))"; using (SqlConnection connection = new SqlConnection(strConnection)) { SqlCommand command1 = new SqlCommand(createTablerow, connection); connection.Open(); command1.ExecuteNonQuery(); } using (SqlConnection cn = new SqlConnection(strConnection)) { cn.Open(); using (SqlBulkCopy copy = new SqlBulkCopy(cn)) { copy.ColumnMappings.Add(0, "code1"); copy.ColumnMappings.Add(1, "date1"); copy.ColumnMappings.Add(2, "open1"); copy.ColumnMappings.Add(3, "high1"); copy.ColumnMappings.Add(4, "low1"); copy.ColumnMappings.Add(5, "close1"); copy.ColumnMappings.Add(6, "vol1"); copy.DestinationTableName = "[" + symbolName + "]"; copy.WriteToServer(dt); } }
I moved some of you code around to make it more efficient : using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); ofd.FileName = ""; ofd.ShowDialog(); string line = null; int i = 0; string strConnection = #"Data Source =.\SQLEXPRESS; AttachDbFilename = C:\USERS\JEF\DOCUMENTS\DATABASE1.MDF; Integrated Security = True; Connect Timeout = 30; User Instance = True"; using (SqlConnection connection = new SqlConnection(strConnection)) { connection.Open(); SqlBulkCopy copy = new SqlBulkCopy(connection); copy.ColumnMappings.Add(0, "code1"); copy.ColumnMappings.Add(1, "date1"); copy.ColumnMappings.Add(2, "open1"); copy.ColumnMappings.Add(3, "high1"); copy.ColumnMappings.Add(4, "low1"); copy.ColumnMappings.Add(5, "close1"); copy.ColumnMappings.Add(6, "vol1"); foreach (string file in ofd.FileNames) { using (StreamReader sr = File.OpenText(file)) { DataTable dt = new DataTable(); while ((line = sr.ReadLine()) != null) { string[] data = line.Split(','); if (data.Length > 0) { if (i == 0) { foreach (var item in data) { dt.Columns.Add(new DataColumn()); } i++; } DataRow row = dt.NewRow(); row.ItemArray = data; dt.Rows.Add(row); } } string symbolName = dt.Rows[1][0].ToString(); string createTable = string.Format("create table [{0}] (code1 VARCHAR(100) COLLATE Arabic_CI_AI_KS_WS,date1 varchar(50),open1 varchar(50),high1 varchar(50),low1 varchar(50),close1 varchar(50),vol1 varchar(50))", symbolName); using (SqlCommand command1 = new SqlCommand(createTable, connection)) { command1.ExecuteNonQuery(); copy.DestinationTableName = "[" + symbolName + "]"; copy.WriteToServer(dt); } } } } } } }
In order to process multiple files at once you will need to use a parallel code. Your use of "for" was a nice try, but "for" loops still run concurrently, which means one at a time. The easiest way to achieve this parallel processing in this case would be to use Parallel.Foreach, here is a Microsoft guide about it: https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop
Insert into table Sqlite C# from CSV File Filters
I created a database with two tables in sqlite. I have a pretty specific CSV file which I have to do an insert in the database. The table 'hyddnev' I need to do insert in column 'Station'(Number of station is first element on each line from file) and 'Dat' (Year is second element of each line from file and day of mounth is third element of each line from file - on the first line is 1st day, second line is 2nd day to 31th day). After days I have 12 columns with values for mounths from January to December. And I need to insert the № of station in Station column in Hydnev table, Date in column Dat in the same table, and Values from January do December in the 12 columns of each line. For now I try to insert № of station and Date, but I have exception: Constraint failed NOT NULL constraint failed: hyddnev.Dat Constraint failed NOT NULL constraint failed: hyddnev.Station CSV file contains years from 1976 to 2015 and each years have 31 lines.. 18050,1976,1,0.390,0.660,0.290,0.740,9.160,1.400,0.670,3.120,0.460,0.420,0.360,0.400, 18050,1976,2,0.390,0.520,0.290,0.740,7.540,1.270,0.670,2.660,0.460,0.420,0.360,0.380, 18050,1976,3,0.390,0.450,0.240,0.660,5.260,1.270,0.670,2.510,0.460,0.420,0.410,0.400, 18050,1976,4,0.390,0.450,0.240,0.660,4.400,1.180,0.620,2.360,0.460,0.410,0.400,0.440, 18050,1976,5,0.390,0.450,0.290,0.660,4.220,1.080,0.620,2.360,0.460,0.410,0.400,4.750, 18050,1976,6,0.520,0.390,0.240,0.580,4.040,1.270,0.620,4.200,0.460,0.410,0.380,2.810, 18050,1976,7,0.390,0.390,0.240,0.520,3.680,37.800,0.620,5.870,0.460,0.400,0.360,1.620, 18050,1976,8,0.390,0.390,0.200,0.580,3.330,22.900,0.580,4.570,0.460,0.380,0.360,0.980, 18050,1976,9,0.390,0.390,0.200,0.660,2.830,11.200,0.580,4.020,0.460,0.360,0.360,0.740, 18050,1976,10,0.390,0.340,0.200,1.380,2.650,8.120,0.580,3.660,0.440,0.360,0.360,0.520, 18050,1976,11,0.340,0.390,0.200,2.260,2.350,5.870,0.580,3.270,0.440,0.360,0.360,0.460, 18050,1976,12,0.340,0.450,0.200,1.700,2.350,4.750,0.580,4.570,0.440,0.360,0.360,0.460, 18050,1976,13,0.340,0.390,0.200,1.590,2.350,3.840,0.540,4.020,0.440,0.340,0.360,0.440, 18050,1976,14,0.340,0.390,0.290,2.120,2.200,3.120,0.540,3.660,0.420,0.340,0.340,0.520, 18050,1976,15,0.290,0.390,0.290,2.400,2.050,2.970,0.540,3.270,0.420,0.400,0.340,0.520, 18050,1976,16,0.290,0.390,0.240,1.590,1.770,2.810,0.540,2.970,0.420,0.360,0.340,0.440, 18050,1976,17,0.290,0.340,0.290,1.170,1.520,2.660,0.540,2.660,0.410,0.360,0.330,0.420, 18050,1976,18,0.290,0.340,0.290,1.170,1.270,2.360,0.540,2.210,0.410,0.410,0.340,0.420, 18050,1976,19,0.240,0.340,0.390,1.170,1.080,2.210,0.540,2.060,0.410,0.410,0.400,0.410, 18050,1976,20,0.290,0.340,0.390,1.010,1.080,2.060,0.520,1.760,0.400,0.400,1.340,0.400, 18050,1976,21,0.290,0.290,0.390,0.920,1.270,1.760,0.520,1.200,0.740,0.400,2.660,0.400, 18050,1976,22,0.340,0.290,0.450,0.820,2.860,1.480,0.520,1.080,0.580,0.380,1.760,0.400, 18050,1976,23,0.340,0.290,0.520,0.740,3.050,1.200,0.520,0.980,0.580,0.380,0.980,0.400, 18050,1976,24,0.340,0.290,0.520,0.660,4.000,0.980,0.540,0.810,0.540,0.380,0.520,0.380, 18050,1976,25,0.340,0.290,0.920,0.740,2.680,0.890,2.810,0.670,0.520,0.360,0.460,0.380, 18050,1976,26,0.390,0.290,1.380,1.380,2.060,0.810,2.510,0.580,0.520,0.360,0.440,0.380, 18050,1976,27,0.740,0.290,1.490,2.570,1.770,0.810,2.510,0.580,0.490,0.360,0.420,0.380, 18050,1976,28,1.280,0.290,1.380,2.730,1.770,0.740,4.750,0.520,0.460,0.360,0.410,0.360, 18050,1976,29,1.010,0.290,1.090,3.610,1.650,0.740,5.480,0.520,0.420,0.360,0.410,0.360, 18050,1976,30,0.820,,0.820,4.000,1.520,0.670,4.210,0.490,0.420,0.360,0.400,0.360, 18050,1976,31,0.660,,0.740,,1.520,,3.660,0.460,,0.360,,0.440, I dont know how to insert into the table, I need to insert after I will use it to draw a graphics with select query. My Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SQLite; namespace WindowsFormsApp1 { public partial class Form1 : Form { private SQLiteConnection Connection; public Form1() { InitializeComponent(); label3.Hide(); label4.Hide(); SQLiteConnection.CreateFile("hydrodb.sqlite"); SQLiteConnection Connection = new SQLiteConnection("Data Source=hydrodb.sqlite;Version=3;"); Connection.Open(); string createTable = ("CREATE TABLE hyddnev (Station UNSIGNED INT(5) NOT NULL, Dat datetime NOT NULL, Stoej int(5) DEFAULT NULL, Vkol UNSIGNED FLOAT(7,3) DEFAULT NULL, PRIMARY KEY (Station, Dat))"); SQLiteCommand createHydDnev = new SQLiteCommand(createTable, Connection); createHydDnev.ExecuteNonQuery(); string createTable2 = ("CREATE TABLE hydmes (Station UNSIGNED INT(5) NOT NULL, Dat datetime NOT NULL, StoejMin smallint(5) DEFAULT NULL, VkolMin UNSIGNED FLOAT(7,3) DEFAULT NULL, StoejSre smallint(5) DEFAULT NULL, VkolSre UNSIGNED FLOAT(7,3) DEFAULT NULL, StoejMax smallint(5) DEFAULT NULL, VkolMax UNSIGNED FLOAT(7,3) DEFAULT NULL, PRIMARY KEY (Station, Dat))"); SQLiteCommand createHydMes = new SQLiteCommand(createTable2, Connection); createHydMes.ExecuteNonQuery(); string deleteTable = ("DELETE FROM hyddnev"); SQLiteCommand deleteHydDnev = new SQLiteCommand(deleteTable, Connection); deleteHydDnev.ExecuteNonQuery(); string deleteTable2 = ("DELETE FROM hydmes"); SQLiteCommand deleteHydMes = new SQLiteCommand(deleteTable2, Connection); deleteHydMes.ExecuteNonQuery(); this.Connection = Connection; } string pathFolder; string pathFolder2; string resultStation; string resultStation2; List<string> resultYears = new List<string>(); List<string> resultYears2 = new List<string>(); private void button1_Click(object sender, EventArgs e) { using (OpenFileDialog dialog = new OpenFileDialog()) { if (dialog.ShowDialog(this) == DialogResult.OK) { string sFileName = dialog.FileName; pathFolder = sFileName; label3.Text = pathFolder; label3.Show(); string[] lines = System.IO.File.ReadAllLines(dialog.FileName); int i = 0; foreach (var line in lines) { var splittedValues = line.Split(','); var firstWord = splittedValues[0]; var firstYear = splittedValues[1]; if (!resultYears.Contains(firstYear)) { resultYears.Add(firstYear); } if (i == 0) { resultStation = firstWord; } else { if (resultStation != firstWord) { MessageBox.Show("Файла с дневни данни трябва да съдържа само една станция!"); return; } } i++; string addDat = ("INSERT INTO hyddnev (Dat) values ('" + resultYears + "')"); SQLiteCommand insertDat = new SQLiteCommand(addDat, Connection); try { insertDat.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } string addStation = ("INSERT INTO hyddnev (Station) values(" + firstWord + ")"); SQLiteCommand insertStation = new SQLiteCommand(addStation, Connection); try { insertStation.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } resultYears.Sort(); } } } private void button2_Click(object sender, EventArgs e) { using (OpenFileDialog dialog = new OpenFileDialog()) { if (dialog.ShowDialog(this) == DialogResult.OK) { string sFileName = dialog.FileName; pathFolder2 = sFileName; label4.Text = pathFolder2; label4.Show(); string[] lines = System.IO.File.ReadAllLines(dialog.FileName); int i = 0; foreach (var line in lines) { var splittedValues = line.Split(','); var firstWord = splittedValues[0]; var firstYear2 = splittedValues[1]; if (!resultYears2.Contains(firstYear2)) { resultYears2.Add(firstYear2); } if (i == 0) { resultStation2 = firstWord; } else { if (resultStation2 != firstWord) { MessageBox.Show("Файла с месечни данни трябва съдържа само една станция!"); return; } } i++; } resultYears2.Sort(); } } } public void label3_Click(object sender, EventArgs e) { } public void label4_Click(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { if (resultStation != resultStation2) { MessageBox.Show("Номера на станцията в единия файл не отговаря на номера на станцията в другият файл!" + Environment.NewLine + Environment.NewLine + "ЗАБЕЛЕЖКА!" + Environment.NewLine + Environment.NewLine + "В двата файла, номера на станцията трябва да бъде един и същ!"); } comboBox1.Items.Add(resultStation); if (string.Join(", ", resultYears) == string.Join(", ", resultYears2)) //if (resultYears.Equals(resultYears2)) { for (int i = 0; i < this.resultYears.Count; i++) { comboBox2.Items.Add(resultYears[i]); } } else { MessageBox.Show("Годините от двата файла не съвпадат."); } } } }
Does your CSV file has header rows? What are the columns in your CSV file? If you have columns headers in CSV file, you can directly load CSV file to the data table and then you can simply query that table. See this: Loading CSV file in data table (then query/apply filters on data table to extract required data) NOTE: Be careful about the "HDR = Yes/NO" and "Data Source=Path to CSV file" in connection string. // using System.Data; // using System.Data.OleDb; // using System.Globalization; // using System.IO; static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader) { string header = isFirstRowHeader ? "Yes" : "No"; string pathOnly = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); string sql = #"SELECT * FROM [" + fileName + "]"; using(OleDbConnection connection = new OleDbConnection( #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly + ";Extended Properties=\"Text;HDR=" + header + "\"")) using(OleDbCommand command = new OleDbCommand(sql, connection)) using(OleDbDataAdapter adapter = new OleDbDataAdapter(command)) { DataTable dataTable = new DataTable(); dataTable.Locale = CultureInfo.CurrentCulture; adapter.Fill(dataTable); return dataTable; } }
Read .csv to datatable and fill a datagridview
I have a .csv file and I'd like to read it into a datagridview (each value into an each column). I read this file with block note and I see that each value is divided by ";" I tried to set a datatable but it's not working. This is my code: string FileName = #"C:\mydir\testcsv.csv"; OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Path.GetDirectoryName(FileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\""); conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + Path.GetFileName(FileName), conn); DataSet ds = new DataSet("Temp"); adapter.Fill(ds); conn.Close(); dataGridView2.DataSource = ds; I don't understand where's the error.
Your code worked for me as it is. I just added one line to the datasource assignment after looking inside the dataset, I saw just one table is inside with name "Table" so I assigned the datamember of the datagridview: dataGridView1.DataSource = ds; dataGridView1.DataMember = "Table"; Anyway if I used ';' separator, all the values were in one column... With ',' comma separator it works ok. The complete code of the form: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string FileName = #"C:\mydir\testcsv.csv"; OleDbConnection conn = new OleDbConnection ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Path.GetDirectoryName(FileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\""); conn.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter ("SELECT * FROM " + Path.GetFileName(FileName), conn); DataSet ds = new DataSet("Temp"); adapter.Fill(ds); conn.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Table"; } } } Contents of the csv file: abc,123 def,456 ijk,789 lmn,111213 For semicolon delimited files you need to add an ini file in your folder containing the csv file. How to do it exactly is described here: How to specify the delimiter when importing CSV files via OLEDB in C# For decimal delimiter symbol you have to add the DecimalSymbol directive to your Jet ini file. See the full ini file capabilities documented in MSDN (https://msdn.microsoft.com/en-us/library/ms709353(v=vs.85).aspx)
I use this function by long, long time, after: yourgrid.datasource = function result. public static DataTable CsvDb(string filename, string separatorChar) { var table = new DataTable("Filecsv"); using (var sr = new StreamReader(filename, Encoding.Default)) { string line; var i = 0; while (sr.Peek() >= 0) { try { line = sr.ReadLine(); if (string.IsNullOrEmpty(line)) continue; var values = line.Split(new[] { separatorChar }, StringSplitOptions.None); var row = table.NewRow(); for (var colNum = 0; colNum < values.Length; colNum++) { var value = values[colNum]; if (i == 0) { table.Columns.Add(value, typeof(String)); } else { row[table.Columns[colNum]] = value; } } if (i != 0) table.Rows.Add(row); } catch (Exception ex) { string cErr = ex.Message; //if you need the message error } i++; } } return table; } Try...
Setting up a chart that displays the number of times a dataset record appears in C#
I am trying to create a chart that when, at the push of a button displays a chart that shows the user the number of times a record has appeared in the dataset/table that it is linked to. Please bare in mind that I have little experience with using Charts in Visual Studios/C#. Currently I am getting this error: Error This is all the code I have so far: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace RRAS { public partial class formRRAS : Form { public OleDbConnection DataConnection = new OleDbConnection(); public formRRAS() { InitializeComponent(); } private void formRRAS_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'database1DataSet.tblReject_test' table. You can move, or remove it, as needed. this.tblReject_testTableAdapter.Fill(this.database1DataSet.tblReject_test); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { this.Close(); } private void btnSearch_Click(object sender, EventArgs e) { //This creates the String Publisher which grabs the information from the combo box on the form. //Select and Dataconnection are also defined here. string Select = "SELECT * FROM tblReject_test"; string DataConnection; string Department = txtDepartment.Text; string Start_Date = txtStart.Text; string End_Date = txtEnd.Text; string Anatomy = txtAnatomy.Text; string RFR = cmbRFR.Text; string Comment = txtComment.Text; //Select defines what should be loaded on to the dataset. if (Department != "") { Select = Select + " WHERE department_id =" + "'" + Department + "'"; if (Anatomy != "") { Select = Select + "AND body_part_examined =" + "'" + Anatomy + "'"; if (Start_Date != "") { Select = Select + " AND study_date =" + "'" + Start_Date + "'"; if (End_Date != "") { Select = Select + " AND study_date =" + "'" + End_Date + "'"; if (RFR != "") { Select = Select + " AND reject_category =" + "'" + RFR + "'"; if(Comment != "") { Select = Select + " AND reject_comment =" + "'" + Comment + "'"; } } } } } } else { Select = "SELECT * FROM tblReject_test"; } //DataConnection connects to the database. string connectiontring= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Database1.mdb"; DataConnection = new OleDbConnection(connectiontring); //The DataAdapter is the code that ensures both the data in the Select and DataConnection strings match. OleDbDataAdapter rdDataAdapter = new OleDbDataAdapter(Select, DataConnection); try { //It then clears the datagridview and loads the data that has been selected from the DataAdapter. database1DataSet.tblReject_test.Clear(); rdDataAdapter.Fill(this.database1DataSet.tblReject_test); } catch (OleDbException exc) { System.Windows.Forms.MessageBox.Show(exc.Message); } } private void btnLoadChart_Click(object sender, EventArgs e) { try { int count = database1DataSet.Tables["tblReject_test"].Rows.Count; DataConnection.Open(); OleDbCommand command = new OleDbCommand(); command.Connection = DataConnection; string query = "SELECT * FROM tblReject_test"; command.CommandText = query; OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { charRejections.Series["RFR"].Points.AddXY(reader["reject_category"].ToString(), reader[count].ToString()); } DataConnection.Close(); } catch (Exception ex) { MessageBox.Show("Error " + ex); } } } }
Your code wouldn't compile as you are assigning a string to DataConnection (instance of OleDbConnection). The correct usage should be as following. string connectiontring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Database1.mdb"; DataConnection = new OleDbConnection(connectiontring)); Also, your code doesn't close Database connection in case of exception. It would be recommended to use the code as shown below. This is taken from MSDN using (OleDbConnection connection = new OleDbConnection(connectionString)) { try { connection.Open(); Console.WriteLine("DataSource: {0} \nDatabase: {1}", connection.DataSource, connection.Database); } catch (Exception ex) { Console.WriteLine(ex.Message); } // The connection is automatically closed when the // code exits the using block. }
C# pass a DataRow[] variable from code behind to .netpage
I am trying to pass a protected DataRow[] msgArray; from code-behind to the .net page. msgArray contains rows from a DB table that I selected, when I do Response.Write(msgArray[0]["comment"]) it outputs correctly what I have stored in the comment column in my DB. The problem is that I cannot do the same in my .net page when I load the page where I do this: <asp:Panel ID="commentSection" runat="server"> <%= msgArray[0]["comment"] %> </asp:Panel> I get a Object reference not set to an instance of an object. What am I doing wrong ? This is my code-behind(.cs) : using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using MySql.Data.MySqlClient; namespace Groups { public partial class Group : System.Web.UI.Page { MySql.Data.MySqlClient.MySqlConnection conn; MySql.Data.MySqlClient.MySqlCommand cmd; MySql.Data.MySqlClient.MySqlDataReader reader; String queryStr; String gname; String gtype; String uname; DataTable group = new DataTable(); DataTable msg = new DataTable(); protected DataRow[] msgArray; protected void Page_Load(object sender, EventArgs e) { String id = Request.QueryString["id"]; if (id != null) { String connString = System.Configuration.ConfigurationManager.ConnectionStrings["GroupsConnString"].ToString(); conn = new MySql.Data.MySqlClient.MySqlConnection(connString); conn.Open(); queryStr = "SELECT g.*, (SELECT COUNT(id) FROM app_groups.users_groups_leg ugl WHERE ugl.id_group = g.id) as member_count FROM app_groups.groups g WHERE g.id = " + id; cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn); group.Load(reader = cmd.ExecuteReader()); var groupArray = group.AsEnumerable().ToArray(); reader.Close(); int member_count = 0; int.TryParse(groupArray[0]["member_count"].ToString(), out member_count); Panel grInfo = new Panel(); grInfo.Controls.Add(new LiteralControl("<br/><div class='panel panel-primary'><div class='panel-heading'><h2>" + groupArray[0]["group_name"] + "</h2></div><div class='panel-body'><span>Categorie: <span class='title'>" + groupArray[0]["group_type"] + "</span></span><br/><span class='membrii'>" + (member_count == 1 ? member_count + " membru" : member_count + " membri") + "</span><br/><span>Fondat pe: " + ConvertUnixTimeStamp(groupArray[0]["founded"].ToString()) + "</span><br/></div></div>")); groupInfo.Controls.Add(grInfo); conn.Close(); showComments(); } } public static DateTime? ConvertUnixTimeStamp(string unixTimeStamp) { return new DateTime(1970, 1, 1).AddSeconds(Convert.ToDouble(unixTimeStamp) + 3600*2); } public DataRow[] showComments() { String id = Request.QueryString["id"]; if (id != null) { String connString = System.Configuration.ConfigurationManager.ConnectionStrings["GroupsConnString"].ToString(); conn = new MySql.Data.MySqlClient.MySqlConnection(connString); conn.Open(); queryStr = "SELECT gc.* FROM app_groups.group_comments gc WHERE gc.id_group = " + id; cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn); msg.Load(reader = cmd.ExecuteReader()); msgArray = msg.AsEnumerable().ToArray(); reader.Close(); Response.Write(msgArray[0]["comment"]); /*Panel grComments = new Panel(); grComments.Controls.Add(new LiteralControl("")); groupInfo.Controls.Add(grComments);*/ } return msgArray; } } }
Create a new class dataAccess.cs using System; using System.Data; namespace Groups { public class dataAccess { public List<string> GetComments() { String connString = System.Configuration.ConfigurationManager.ConnectionStrings["GroupsConnString"].ToString(); conn = new MySql.Data.MySqlClient.MySqlConnection(connString); try { MySql.Data.MySqlClient.MySqlDataReader reader; DataTable msg = new DataTable(); conn.Open(); List<string> comments = new List<string>(); queryStr = "SELECT gc.* FROM app_groups.group_comments gc WHERE gc.id_group = " + id; cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn); msg.Load(reader = cmd.ExecuteReader()); foreach(DataRow dr in msg.Rows) { comments.Add(dr["comment"]); } reader.Close(); return comments; } catch (Exception ex) { //throw ex; } } } } In the ASPX page <asp:Panel ID="commentSection" runat="server"> <% var data = Groups.dataAccess.GetComments(); foreach(string c in data) { Response.Write("<p>" + c + "</p>"); } %> </asp:Panel>
According to ASP.NET Page Life cycle, your method showComments() will be called after the <%= msgArray[0]["comment"] %> part. Hence it won't be available there. Best way is to have a control like Label in your .aspx page and update the text property from showComments() method.