I'm using a dataset to show data on crystal report without using an SQL server.
My problem is that data is not inserted into the table.
Here is my code:
try
{
DataSet2 ds1 = new DataSet2();
DataTable t1 = ds1.Tables.Add("report1");
t1.Columns.Add("mtrno", Type.GetType("System.String"));
t1.Columns.Add("pf1", Type.GetType("System.String"));
t1.Columns.Add("pf2", Type.GetType("System.String"));
t1.Columns.Add("pf3", Type.GetType("System.String"));
t1.Columns.Add("pf4", Type.GetType("System.String"));
t1.Columns.Add("pf5", Type.GetType("System.String"));
t1.Columns.Add("pf6", Type.GetType("System.String"));
t1.Columns.Add("pf7", Type.GetType("System.String"));
t1.Columns.Add("rel", Type.GetType("System.String"));
//t3.Columns.Add("r_no",Type.GetType("System.String"));
//t3.Columns.Add("date", Type.GetType("System.String"));
//t3.Columns.Add("type", Type.GetType("System.String"));
//t3.Columns.Add("const", Type.GetType("System.String"));
//t3.Columns.Add("volt", Type.GetType("System.String"));
//t3.Columns.Add("class", Type.GetType("System.String"));
//t2.Columns.Add("rel", Type.GetType("System.String"));
DataRow r1;
// DataRow r3;
textBox1.Clear();
DirectoryInfo d_info = new DirectoryInfo("D:\\");
FileInfo[] f_info = d_info.GetFiles("*.txt");
foreach (FileInfo fi in f_info)
{
string fname = @"D:\\" + Path.GetFileNameWithoutExtension(fi.Name) + ".txt";
if (fname.Contains(listBox1.SelectedItem.ToString()))
{
StreamReader sread1 = new StreamReader(fname);
string line = null;
while ((line = sread1.ReadLine()) != null)
{
if (line.Contains("[Report Header]|" + comboBox3.SelectedItem.ToString()))
{
line = line.Replace("[Report Header]|", "");
string[] r_words = line.Split('|');
line = sread1.ReadLine();
if (line.Contains("[Field Heading]|"))
{
string[] f_words = line.Split('|');
foreach (string s in f_words)
{
textBox1.Text = textBox1.Text + s;
}
}
textBox1.Text = textBox1.Text + Environment.NewLine;
for (int i = 0; i < 32; i++)
{
line = sread1.ReadLine();
if (line.Contains("[Meter_Record]|") || line.Contains(comboBox4.SelectedItem.ToString()))
{
line=line.Replace("[Meter_Record]|","");
string[] m_words = line.Split('|');
r1 = t1.NewRow();
r1["mtrno"] = m_words[0].ToString();
r1["pf1"] = m_words[1].ToString();
r1["pf2"] = m_words[2].ToString();
r1["pf3"] = m_words[3].ToString();
r1["pf4"] = m_words[4].ToString();
r1["pf5"] = m_words[5].ToString();
r1["pf6"] = m_words[6].ToString();
r1["pf7"] = m_words[7].ToString();
r1["rel"] = "yes";
//for (int j = 0; j < m_words.Length;j++ )
//{
// textBox1.Text = textBox1.Text + m_words[j];
//}
}
//textBox1.Text = textBox1.Text + Environment.NewLine;
}
}
}
}
}
CrystalReport2 objRpt1 = new CrystalReport2();
//objRpt.SetDataSource(ds.Tables["h_report"]);
objRpt1.SetDataSource(ds1.Tables["report1"]);
crystalReportViewer1.ReportSource = objRpt1;
crystalReportViewer1.Zoom(1);
crystalReportViewer1.Refresh();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Generally to add columns look at the solution here , and to add data:
DataRow anyRow = ds1.report1.NewRow();
anyRow.mtrno = 123;
anyRow.pf1 = "abc";
report1.Rows.Add(anyRow);
EDIT: (in response to your comment)
in your code you are not adding the new row, you define one but never add it ie
report1.Rows.Add(anyRow);
look inside your for loop ie
r1 = t1.NewRow();
r1["mtrno"] = m_words[0].ToString();
but you never add this new row to the table. Which is why i gave you an example of how to 'Add' a row.
you are not adding t1
(DataTable) into dataset
.
Add this one line
ds1.Tables.Add(t1);
before
CrystalReport2 objRpt1 = new CrystalReport2();
The problem is that before this point you have to add rows to the data table and the data table to the data set.
objRpt1.SetDataSource(ds1.Tables["report1"]);
You are making a dataset, datatable, datarows and putting values in those rows. What you forget is that the datatable does not know that you added data to a row, same as the dataset who dint know you did anthing with a datatable. To let the upper level actually contain the data you must add it there as well.
Use this code after you enter the values in the row.
// Add row to the table.
t1.Rows.Add(r1);
// Now the table contains a single row.
Use this code after you complete the datatable (since you have only one that can be just before you create the crystal report.
// Add table to the upper level data set.
ds2.Tables.Add(t1);
// Now the data set contains a single data table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.