Below is my code.
DataSet ds = new DataSet();
ds = dsComplaints;
DataTable dt = new DataTable();
dt = ds.Tables[0];
DataRow dr = dt.NewRow();
dr.ItemArray = new object[] {"--Select Complaint--" };
dt.Rows.InsertAt(dr, 0);
dsComplaints is globally declared which has the main data. I am copying it to a local DataSet ds. Then I assign the table from ds to a datatable. I am adding a row(select complaint) in data table. But when the insertion happens, my main dataset i.e. dsComplaints is also added with a row. How can i avoid that. I don't know why this is happening.
You can use the Copy method to create a new copy of same DataSet instead of using the reference only of the original one to avoid this :
DataSet ds = dsComplaints.Copy();
From MSDN DOCS this is what Copy does:
Copies both the structure and data for this DataSet.
Now you can do whatever is needed with the copy and it would not effect the original version of DataSet.
Related
I have a Dataview that is being populated with 3 rows of data from a stored procedure. I want to put that table in a Dataset. I read, but I may be wrong, is to go from DataView to DataTable To DataSet.
Below is my code. When I hit the if statment, it shows that my DT2.Rows.Count = 3, so it runs DS.Tables.Add(DT2); then throws the following Error. "NullReferenceException was unhandled by user code" Object reference not set to an instance of an object.
I was wondering how does one get a DataView to a Dataset? Thank you for any suggestions.
DataSet DS;
DataView DV = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataTable DT2 = DV.ToTable();
if (DT2 != null & DT2.Rows.Count > 0)
{
DS.Tables.Add(DT2);
}
Your DataSet is null. Just initiate it and you should be good to go:
DataSet DS = New DataSet();
The rest of your code is correct.
DataSet DS=new DataSet();
DataView DV = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DS.Tables.Add(DV.ToTable());
I want to know how to update a Datatable which is inside a dataset.I have a datatable in which i have details of some item.Now i want to add this into a dataset for some purpose and update it.Give me some suggesions to solve this..
this is my code:
DataRow dr;
dr = Basket_DataTable.NewRow();
Basket_DataTable.Rows.Add(dr);
dr["PId"] = getPId.ToString();
dr["ProductName"] = getProductName.ToString();
dr["ImagePath"] = getImagePath.ToString();
dr["ProductPrice"] = getProductPrice.ToString();
dr["Quantity"] = getQuantity.ToString();
dr["ProductDescription"] = getProductDescription.ToString();
dr["TotalPrice"] = getProductPrice.ToString();
Basket_DataTable.AcceptChanges();
Basket_DataTable is my datatable which i need to add to a dataset and update..
I believe you want to add new Rows to your existing DataTable in your DataSet. Instead of creating a new DataTable, your Basket_DataTable should refer to your data table in the data set.
Something like.
//Create new Row from your DataTable in DataSet
DataRow dr = yourDataSet.Tables["Basket_DataTable"].NewRow();
// here you can refer to your datatable with the index as well
//e.g. yourDataSet.Tables[0]
Basket_DataTable.Rows.Add(dr);
dr["PId"] = getPId.ToString();
dr["ProductName"] = getProductName.ToString();
dr["ImagePath"] = getImagePath.ToString();
dr["ProductPrice"] = getProductPrice.ToString();
dr["Quantity"] = getQuantity.ToString();
dr["ProductDescription"] = getProductDescription.ToString();
dr["TotalPrice"] = getProductPrice.ToString();
//Remember to add your row to the table.
yourDataSet.Tables["Basket_DataTable"].Rows.Add(dr);
In your current code you are not adding the new row to the datatable. Remember to include the row in the datatable.
If the DataTable is already in an existing DataSet, but you want to add it to another one, you need to create a copy first - A DataTable instance can only have one parent DataSet.
DataTable Basket_DataTable_copy = Basket_DataTable.Copy();
yourDataSet.Tables.Add(Basket_DataTable_copy);
Then you can do your updates on Basket_DataTable_copy
see DataTable.Copy on MSDN
A DataGridView bound by this code does not display information as expected:
dataGridView1.DataSource = ds;
here's the code for ds:
public DataSet ConnectandReadList()
{
DataSet ds = new DataSet();
string connection_string="Data Source=hermes;database=qcvalues; Integrated Security=SSPI;";
using (var myConnection = new SqlConnection(connection_string))
{
myConnection.Open();
var command = new SqlCommand(InitializeQuery(), myConnection);
var adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
}
return ds;
}
Try binding to the table inside the dataset: dataGridView1.DataSource = ds.Tables[0];
From the documentation for the DataGridView.DataSource property, you can also bind to a DataSet and use the DataMember property:
When binding to a data source that contains multiple lists or tables, you must set the DataMember property to a string that specifies the list or table to bind to.
I think in this case the table name would be "Table" since you are not naming it explicitly.
Is ds a DataSet?
If so, try setting your DGV's DataMember to a DataTable within the DataSet or specifying your DataSet's DataTable for the DataSource.
hai friends
I am one dataset like this ds like three columns this:
tblkey Empkey Empname
T101 E10 Natraj
T102 E11 Siva
T103 E14 ganesh
I am having another dataset ds1 only two columns like this:
Empkey Empname
E10 karthi
E11 thriu
E13 maran
i waant merge the dataset and check the values while checking if ds is not having E13 it should bind and show the result like this ds
tblkey Empkey Empname
T101 E10 Natraj
T102 E11 Siva
T103 E14 ganesh
E13 maran
here 'tblkey' comes empty
how to do:
To replicate your example entirely:
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
ds1.Tables.Add(new DataTable());
ds2.Tables.Add(new DataTable());
ds1.Tables[0].Columns.Add("tblkey");
ds1.Tables[0].Columns.Add("empkey");
ds1.Tables[0].Columns.Add("empname");
ds2.Tables[0].Columns.Add("empkey");
ds2.Tables[0].Columns.Add("empname");
ds1.Tables[0].Rows.Add("T101", "E10", "Natraj");
ds1.Tables[0].Rows.Add("T102", "E11", "Siva");
ds1.Tables[0].Rows.Add("T103", "E14", "ganesh");
ds2.Tables[0].Rows.Add("E10", "karthi");
ds2.Tables[0].Rows.Add("E11", "thriu");
ds2.Tables[0].Rows.Add("E13", "maran");
// primary keys must be set in order for the merge to work
ds1.Tables[0].PrimaryKey = new DataColumn[] { ds1.Tables[0].Columns["empkey"] };
ds2.Tables[0].PrimaryKey = new DataColumn[] { ds2.Tables[0].Columns["empkey"] };
// this is the critical line
ds1.Merge(ds2, true, MissingSchemaAction.Add);
Adding the missing schema (in this case, the tblkey column) is achieved by setting the third parameter correctly.
I am trying to add data to a newly created column...
DataSet ds = new DataSet;
ds.Tables[0].Columns.Add("Count");
Data.DataSource = ds;
Data.DataBind();
Where do I add, a hardcoded count? (I want count column to have '4')
You add data to rows - so:
DataSet ds = new DataSet();
DataTable table = ds.Tables.Add();
table.Columns.Add("Count", typeof(int));
table.Rows.Add(4);
However, there are probably easier options; a DataSet seems massively overkill to bind a single value.
It technically is a repeater function but if I get the hardcoded function... I can make the code to make it flexible. I got it to work. I was adding a column to an existing table. This is what I did...
for int i=0; i<ds.tables[0].Rows.Count; i++)
{
ds.tables[0].Rows[i]["Count"] = "4";
}