system.argumentexception column does not belong to table - c#

I have a Defined Dataset with table Customer in it. I'm trying to add a row to this Dataset but I'm getting an error below is my code
DataSet ds = new DataSet1();
DataTable dt = new DataTable("Customer");
using (StreamReader reader = new StreamReader(fileStream))
{
while(reader.Peek() >= 0)
{
string row = reader.ReadLine();
string[] content = row.Split('|');
DataRow dr = dt.NewRow();
dr["mother_name"] = content[0]; // I'm getting an error here.
dr["beneficial_name"] = content[1];
dr["industry_code"] = content[2];
dt.Rows.Add(dr);
}
}
filestream.Close();
ds.Tables.Add(dt);
cryRpt.SetDataSource(ds);
So I'm getting an exception error in line dr["emp_lastname"] = content[0]; the key exist in the table in my dataset. So I assumed that when I do dt.NewRow() it will create an instance of my dataset table row. How do I solve this? I just want to populate a row in my DataSet Table. Below is the screenshot of my Dataset

You have a Dataset instance in ds and a DataTable instance in dt. That DataTable instance is not linked to your Dataset in anyway. The Datatable has therefore no columns.
Do a dt=ds.Tables["Customer"]; instead to get an instance of the Datatable that conforms to the schema in your typed dataset.

Related

How to transfer some selected row from one Dataset to another in c#

I have two dataset
DataSet dsvoucherget = new DataSet();
dsvoucherget = queries.getDetails(sqlVoucherget);
and
DataSet dsbalamount = new DataSet();
what I want to do is:
dsbalamount.Tables.Add(dsvoucherget.Tables[0].Select("RegnNo=EM01224").CopyToDataTable());
but I'm not able to do so please help
var result = dsvoucherget.Tables[0].AsEnumerable().Where(x => x["RegnNo"].ToString() == "EMO1224");
if (result.Any())
{
DataTable tbl = result.CopyToDataTable();
}
You could use LINQ. You need System.Linq reference. If you want you can add the DataTable to DataSet after that.

c# split one datatable to several into dataset

I have correct source DataTable "sourceDataTable" and I call method to split it into several and store the result into DataSet "ds":
DataSet ds = MyClass.SplitDataTables(sourceDataTable);
Here is the method MyClass.SplitDataTables():
public static DataSet SplitDataTables(DataTable sourceDataTable)
{
using (DataSet dsOut = new DataSet())
{
DataTable dt1 = new DataTable;
DataTable dt2 = new DataTable;
DataTable dt3 = new DataTable;
dt1 = sourceDataTable.Clone();
dt2 = sourceDataTable.Clone();
dt3 = sourceDataTable.Clone();
foreach (DataRow row in sourceDataTable.Rows)
{
//column is for example "City" and some row has "Boston" in it, so I put this row into dt1
if (row["ColumnName"].ToString() == "something")
{
dt1.ImportRow(row);
}
else if (...)
{ } //for other DataTables dt2, dt3, etc...
else .......... ;
}
//here I put resulting DataTables into one DataSet which is returned
string[] cols= { "dt1", "dt2", "dt3" };
foreach (string col in cols)
{
dsOut.Tables.Add(col);
}
return dsOut;
}
}
So with this returned DataSet I display new Windows each with one DataTable
foreach (DataTable dtt in ds.Tables)
{
string msg = dtt.TableName;
Window2 win2 = new Window2(dtt, msg);
win2.Show();
}
All I get shown is Windows with placeholder for "empty DataGrid"
Windows code is correct, as it works whith "unsplit DataTable".
I assume code in splitting DataTables is all wrong as it does not output DataSet with filled DataTables. I will greatly appreciate any help on this issue. Thank you!
You don't need a for loop here
Replace below code
string[] cols= { "dt1", "dt2", "dt3" };
foreach (string col in cols)
{
dsOut.Tables.Add(col);
}
With this
dsOut.Tables.Add(dt1);
dsOut.Tables.Add(dt2);
dsOut.Tables.Add(dt3);
Thanks to #Krishna I got this solved. So if you ever encounter similar problem here are 2 things to note:
string[] cols = { "dt1", "dt2", "dt3", ... };
foreach (string col in cols)
{
dsOut.Tables.Add(col);
}
This cycle does not have access to objects of DataTables with same name and writes only empty DataTables into DataSet collection (regardless of same name!).
If you create new DataTable and you will be making it a clone of another DataTable, dont bother yet with setting its name.
DataTable dt1 = new DataTable();
Make new DataTable of same format as source DataTable:
dt1 = sourceDataTable.Clone();
dt2 = sourceDataTable.Clone();
//etc...
Now you have to set unique DataTable names to each DataTable cloned from source DataTable:
dt1.TableName = "Name1";
dt2.TableName = "Name2";
//and so on
Now all works as intended.

Run time error 'Cannot find column 0'

Run time error Cannot find column 0. below is my code
string connectiostring = (string)ConfigurationSettings.AppSettings["NorthwindConnectionString"];
SqlConnection conn = new SqlConnection(connectiostring);
SqlCommand cmd = new SqlCommand("select * from Employees", conn);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet data = new DataSet();
adapter.Fill(data,"Employees");
data.Tables["Employees"].Columns.Add("Testcolumn");
DataTable t1 = new DataTable("Employees");
DataRow newrow = t1.NewRow();
newrow[0] = "10";\\this the line i am getting error
newrow[1] = "Pradeep";
newrow[2] = "Kumar";
data.Tables["Employees"].Rows.Add(newrow);
GridView2.DataSource = data;
GridView2.DataBind();
Please help me
Thanks,
You haven't added the columns to the DataTable.
t1.Columns.Add(new DataColumn
{
DataType = string,
ColumnName = "First Name"
});
repeat this for each column supplying the correct type for each.
Create a function that adds columns before trying to add rows.. Call the method at InitializeComponents..
ex.
private void InitTbl(DataTable myTbl)
{
myTbl.Columns.Add(new DataColumn("id"));
myTbl.Columns.Add(new DataColumn("fname"));
myTbl.Columns.Add(new DataColumn("lname"));
}
You need to add some columns to the table first:
DataTable t1 = new DataTable("Employees");
t1.Columns.Add("column1", typeof(string));
t1.Columns.Add("column2", typeof(string));
t1.Columns.Add("column3", typeof(string));
DataRow newrow = t1.NewRow();
...
I think maybe this is actually your problem.
DataTable t1 = new DataTable("Employees")
This creates a brand new table object that is not part of your dataset.
So you should be replace it with this
DataTable t1 = data.Tables["Employees"]
Which gets the table from the dataset and points the t1 variable at that table.

how to create crystal report using datagridview values without using database in C#?

I am working Windows form Application in c#.I like to create crystal report from datagridview values instead of using database values. How can i do this,Is it possible to do this.how can add the values in crystal report dynamically
You could create a DataSet and populate it with the values from the DataGridView. You can then bind the Crystal Report to the DataSet.
Something along the lines of:
DataSet ds = new DataSet();
ds = FetchDataFromGrid();
CrystalReport myReport = new CrystalReport()
myReport.SetDataSource(ds);
crystalReportViewer1.ReportSource = myReport
To retrieve the rows from a DataGridView you will need something like this:
DataSet ds = new DataSet();
DataTable dt = new DataTable();
foreach (DataGridViewRow item in this.dataGridView1.Rows)
{
DataRow dr = dt.NewRow();
if (item.DataBoundItem != null)
{
dr = (DataRow)((DataRowView)item.DataBoundItem).Row;
dt.ImportRow(dr);
}
}
ds.Tables.Add(dt);
SqlDataAdapter da = new SqlDataAdapter("select * from Sells", con.connection);
da.SelectCommand.CommandType = CommandType.Text;
DataSet ds = new DataSet();
con.connection.Open();
da.Fill(ds);
con.connection.Close();
TotalSellsReport rpt = new TotalSellsReport();
rpt.SetDataSource(ds);
SellsPrinting sp = new SellsPrinting();
sp.crystalReportViewer1.ReportSource = rpt;
sp.Show();

how to add datatable to dataset in asp.net

DataSet ds = new DataSet();
DataRow[] foundRows;
foundRows = ds.Tables[0].Select("MerchantName LIKE '%'", "MerchantName");
DataTable DataTable2 = new DataTable();
DataTable2 = ds.Tables[0].Clone();
foreach (DataRow dr in foundRows)
{
DataTable2.ImportRow(dr);
}
ds.tables[0].rows.add(DataTable2); // error table already exists.
Loadimages(ds);
hi all,
upto foreach loop everything is working fine. In loadimages method i have to dataset. but i have the data in datatable. If i add datatable to dataset i am getting the error saying table already exists.
Please help me regarding this.
Thanks in advance..
I don't understand why you're trying to add a DataTable into another DataTable. Surely your code should be as follows:
DataSet ds = new DataSet();
DataRow[] foundRows;
foundRows = ds.Tables[0].Select("MerchantName LIKE '%'", "MerchantName");
DataTable DataTable2 = new DataTable();
DataTable2 = ds.Tables[0].Clone();
DataTable2.TableName = "DataTable2";
foreach (DataRow dr in foundRows)
{
DataTable2.ImportRow(dr);
}
ds.Tables.Add(DataTable2);
Loadimages(ds);
The reason for your error is because your DataTables within the DataSet must have unique names.

Categories