A relation named ... already belongs to this DataSet - c#

I can't fix this error that I get A Relation named 'PlaneAirline' already belongs to this DataSet. I have tried to change the name of the relation but I get the same error
Here is my code:
private void getData()
{
SqlDataAdapter parentDataAdapter = new SqlDataAdapter("select * from Airline", connection);
parentDataAdapter.Fill(ds, "Airline");
SqlDataAdapter childDataAdapter = new SqlDataAdapter("select * from Plane", connection);
childDataAdapter.Fill(ds, "Plane");
DataColumn parentColumn = ds.Tables["Airline"].Columns["airline_id"];
DataColumn childColumn = ds.Tables["Plane"].Columns["airline_id"];
rel = new DataRelation("PlaneAirline", parentColumn, childColumn);
ds.Relations.Add(rel);
parentBindingSource.DataSource = ds;
parentBindingSource.DataMember = "Airline";
childBindingSource.DataSource = parentBindingSource;
childBindingSource.DataMember = "PlaneAirline";
}
private void dg_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void AirlineReservation_Load(object sender, EventArgs e)
{
parentDataGridView.DataSource = parentBindingSource;
childDataGridView.DataSource = childBindingSource;
getData();
}
Could you please help me

I think your problem is that you are calling GetData() method on CellContentClick event. This event will get fired when the content within a cell is clicked. Hence whenever you are clicking the cell your code is trying to add a relation which is already present when you first clicked a cell.
Probably you need to move the GetData() call to Form Load as given in the tutorial you have given.
Hope this helps.

On every click in your DataGrid (I'm assuming that dg_CellContentClick is tha click handler for the data grid cell) you're calling getData, where you try to add the relation PlaneAirlineover and over again.
Try to move the lines
rel = new DataRelation("PlaneAirline", parentColumn, childColumn);
ds.Relations.Add(rel);
to where you initially create or define your variable ds (I think it's a DataSet).

Related

Calling textbox information from one form to another

I have 2 forms: The first form has textboxes that display different task names and times spent on each task. The second form has a datagridView. Using:
public void DataTableTest()
{
//Create DataTable
DataTable dt = new DataTable();
//Add Columns
dt.Columns.Add("Task Name", typeof(string));
dt.Columns.Add("Time Worke (HH:mm:ss)", typeof(string));
//Add Rows
dt.Rows.Add();
dataGridView1.DataSource = dt;
}
I am able to get the data table layout the way I want. My issue is, I want to pull the textbox.text from Form1 to populate the [0][0] cell of the data table. Something like:
//Add Rows
dt.Rows.Add();
dt.Rows[0][0] = Form1.tasktextbox1.text;
dataGridView1.DataSource = dt;
But this is not working. I am getting the following from my error list when I try to start the program:
'TaskTracker.Form1.TaskTextBox1' is inaccessible due to its protection level.
Thank you in advance for the assistance. If you have any questions ask, first time posting and new to programming.
The error describes itself: TaskTextBox1 is a private member of Form1 and you cannot call it on another class. Add a property to Form1:
Public string task1text { get { return this.textbox1.text; }}
and then use it on the other form:
dt.Rows[0][0] = Form1.task1text;
I was able to complete this task with:
public static string task1text;
public void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
task1text = TaskTextBox1.Text;
ReportForm RF = new ReportForm();
RF.Show();
}
on form1 and using Roozbeh's suggestion on the second form:
dt.Rows[0][0] = Form1.task1text;
Thank you all for your assistance. Slowly figuring it out! :)

Assign a value to drop down list on page_load

I'm trying to assign value to a drop down list on the page_load, but it's not automatically get selected when the page loads up. But when I try to select another value from the drop down list, then the value assigned to it originally gets selected. I think it's something to do with the PostBack, My code is,
if (!string.IsNullOrEmpty(Request.QueryString["selectedReg"]))
{
string selectedReg = Request.QueryString["selectedReg"];
ddlVehicleReg.SelectedIndex = ddlVehicleReg.Items.IndexOf(ddlVehicleReg.Items.FindByText(selectedReg));
}
If I use if(!IsPostBack) still no luck, Any ideas? Thanks a lot in advance
The code block you have written is working. No need to check for postback. Be careful about your querystring. It looks like these pictures for my tests.
After your comment I changed dropdown item list. It is getting data from database. And I called this method in Page_Load
protected void Page_Load(object sender, EventArgs e)
{
FillDropDown();
if (!string.IsNullOrEmpty(Request.QueryString["selectedReg"]))
{
string selectedReg = Request.QueryString["selectedReg"];
ddlVehicleReg.SelectedIndex = ddlVehicleReg.Items.IndexOf(ddlVehicleReg.Items.FindByText(selectedReg));
}
}
protected void FillDropDown()
{
using (SqlConnection con= new SqlConnection("server=.;database=StackTest;integrated security=true") )
{
SqlDataAdapter adp = new SqlDataAdapter("select * from Test", con);
DataTable dt = new DataTable("Test");
adp.Fill(dt);
ddlVehicleReg.DataValueField = "Id";
ddlVehicleReg.DataTextField = "Value";
ddlVehicleReg.DataSource = dt;
ddlVehicleReg.DataBind();
}
}

Why do different Session Variables changes value when one session is updated?

Background:
Two buttons on a ASP.NET page.
Button 1:
Data is loaded into a DataSet.
2 Session variables, Session["a"] and Session["b"] are assigned the DataSet.
Button 2:
Session["a"] is cast to a DataSet. A record is deleted from the DataSet.
Issue:
BEFORE the AcceptChanges() function is called, both Session variables are changed to reflect the delete action (Rows.Count is 1 less). Why does this happen?
Surely the DataSet "ds" could not exist anymore?
protected void Button1_Click(object sender, EventArgs e)
{
string StringContainingSQLConnection = #"server=someserver;database=Standby;uid=StandbyUser;password=pass;";
SqlDataAdapter adp = new SqlDataAdapter("select * from CoreData", StringContainingSQLConnection);
DataSet ds = new DataSet();
adp.Fill(ds);
Session["a"] = ds;
Session["b"] = ds;
}
protected void Button2_Click(object sender, EventArgs e)
{
DataSet ds = ((DataSet)Session["a"]);
foreach (DataRow drw in ds.Tables[0].Rows)
{
drw.Delete();
ds.AcceptChanges();
Session["a"] = ds;
break;
}
}
I removed the conditional IF statement in the foreach loop for clarity and just delete the first record to prove the point.
Session a and Session b hold a reference to the location of the dataset in memory. So essentially a and b are just two different names for the same thing.
https://msdn.microsoft.com/en-us/library/490f96s2.aspx
If you wish for b to remain unchanged, you can create two datasets with the same initial data. Assign one set to a and one set to b.

how to subtract two datatables?

I want to subtract two datatables. What i want is that in below Image.
I have the following code and i stacked at last point. Here is what i want to do in the code.
When i click Button1, it will get current data into firstDsData. A few seconds later when i click Button2, it will upload the current data to the secondDsData and merge it together. Then they will merge together into FinalDsData. Differences datatable will get the changes from finaldsdata.
The problem is in the last section. FinalDsData.GetChanges() doesn't work. It says nullreferenceexception. I also tried get changes with RowState.Unchanged/Modified(). They also didn't work.
DataTable firstDsData = new DataTable();
DataTable secondDsData = new DataTable();
DataTable finalDsData = new DataTable();
DataTable DifferenceDataTable = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
this.dataTable1TableAdapter.Fill(this.dataWE.DataTable1);
}
private void button1_Click(object sender, EventArgs e)
{
firstDsData = dataTable1TableAdapter.GetData();
}
private void button2_Click(object sender, EventArgs e)
{
secondDsData = dataTable1TableAdapter.GetData();
finalDsData.Merge(firstDsData);
finalDsData.AcceptChanges();
finalDsData.Merge(secondDsData); //Until here i can get data into finalDsData. No Problem.
DifferenceDataTable = finalDsData.GetChanges(); // Problem in here.Nullreferenceexception
ultraGrid2.SetDataBinding(DifferenceDataTable, DifferenceDataTable.TableName);
}
If your only goal is to show changes on a grid then you can use LINQ to find difference between two tables like:
var changes = (from dr1 in firstDsData.AsEnumerable()
from dr2 in secondDsData.AsEnumerable()
where dr1.Field<string>("Name") == dr2.Field<string>("Name")
select new
{
Name = dr1.Field<string>("Name"),
Value = dr1.Field<decimal>("Value") - dr2.Field<decimal>("Value")
}).ToList();
To show it to your grid you can do:
ultraGrid2.DataSource = changes;
If you want a DataTable for the output then you can convert your LINQ results to a DataTable, see Convert select new to DataTable?

Refresh one row in Dataset from Database before show edit form

I have a DataSet + TableAdapter and bound dataGridView. I have an edit button which opens a new form with details to edit (WinForms). How do I refresh one row (selected one) in the Dataset and in dataGrid from database before opening the new form?
Example: Two users A and B. User A changed record ID(10) and user B still has the old value in record ID(10). User B presses edit button and should get fresh data from database (data after change made by user A).
string sql = "SELECT * FROM Orders";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
da.Fill(ds, "Orders");
connection.Close();
dataGridView1.DataSource = ds;
.....
private void button1_Click(object sender, EventArgs e)
{
//?
//refresh selected row in datagrid (from current database record)
//?
EditForm()
}
Create a new query in your TableAdapter that should only get one row and call it with just the primary key of the row you already have.
Plug the result into a new DataSet and grab the first row (after checking for Rows.Count() > 0)
I would enclose the populating the gridview bit in it's own method say something like fill_grid(). Then just call the method in the edit link click event. Then whatever has changed will update. Then add the method to all the other button link events too. Then every time the user is clicking a link the grid "refreshes" essentially.
For those who is looking for the answer in 2019,
assuming dataAdapter is a defined earlier class member
private void ButtonUpdate_Click(object sender, RoutedEventArgs e)
{
if (dataGrid.SelectedItem is DataRowView view)
{
DataRow currentRow = view.Row;
DataTable table = currentRow.Table;
dataAdapter.Fill(table.Rows.IndexOf(currentRow), 1, table);
}
}

Categories