I want to do a login form in C# and a problem occurred. I want to search all rows of table but I get an error. I have done it two minutes earlier and it worked. Tried to re-do it and it failed. I have no idea how to set up the reference to database object (I didn't do it the first time and somehow it worked).
Line of code with error :
foreach (DataRow row in LoginsDataSet.Loginy)
error text:
An object reference is required for the non-static field, method, or...
As you mentioned in comment your DataSet instance variable is dane, then you must use something like this:
foreach(DataRow row in dane.Loginy.Rows)
{
....
}
in case if you have strongly typed dataset you may have something like this:
foreach(LoginsDataSet.LoginyRow row in dane.Loginy)
{
....
}
Related
I wrote an application which is working with strongly typed datasets and adapters.
First it reads data from database and store it to the typed datatable.Second this datatable processed by this application (add, delete or update rows) and last writes updated data back to the database.
On the step of updating sometimes application causes the exception of
Violation of PRIMARY KEY constraint 'PK_GPS_Events_History'. Cannot insert duplicate key in object 'dbo.GPS_Events_History'. The duplicate key value is (16805552).
The statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}
Table GPS_Events_History at the moment of exception contains about ~30000 rows.
How to filter data in datatable to look at this single row with ID=16805552? I tried to filter it by following code:
dataTable.where(row => row.ID == 16805552)
but VS2010 didn't allow me to enter any LINQ expression in debugger variables window.
Make the result you want to debug save to a variable, then inspect the variable:
var testData = dataTable.Where(row => row.ID == 16805552);
//once the line above has executed, quickwatch the "testData" variable
You can't put lambdas in a quickwatch window, so this is the next best thing.
I typed following command in quick watch window and it solved the problem:
Select("ID = 16805552")
During a gridview => database update function, I use a column-by-column conversion to string in order to pass data back to my database as a whole command string. I'm aware that this sounds convoluted, so here is an example:
Classes in use:
InvoiceHandler.cs
Default.aspx.cs
Since this is a rather large snippet, here is a pastebin:
Default.aspx.cs: http://pastebin.com/Y3fJZ36Z
InvoiceHandler.cs: http://pastebin.com/ZsdAnDxr
At the first point of conversion (invoiceTableEdited.Columns["Column1"].ColumnName = "#K_INV";) I get a NullReferenceException error, assumedly because the method call in Default.aspx.cs
handler.invoiceTableEdited = ViewState["invoiceTable"] as DataTable;
handler.invoiceTableEdited.Rows[row.RowIndex]["K_INVOICE"] = sK_INVOICE;
appears to be having trouble.
What must I do to resolve this?
I'll be honest, it's not very clear how you're trying to create the column collection for the table 'InvoiceTableEdited'. What it looks like you're doing in OnUpdate is you are assigning a string variable to a row in the table with a given index and a column that doesn't currently exist. Your basically saying put this string into a cell with a row number of 'x' and a column name of "column1". At this point "column1" doesn't exist.
I'd create the columns you need first in InvoiceHandler.cs like this (assumes they're string):
invoiceTableEdited.Columns.Add("MyColumn1", typeof(string));
invoiceTableEdited.Columns.Add("MyColumn2", typeof(string));
Obviously if you have loads of columns and you don't care about their names then just create a loop and add them that way. This will give them the naming convention "ColumnN", where 'N' is the number. You can then assign a name to them by referencing there name ("Column1" for example) or using their index.
What are the essential controls before getting a piece of data from a row of a table of a dataset in c#? In order to get rid of the errors while converting a nullable data into decimal, or getting a coloumn that may not be in the datasource any more?
What do I do to validate the existence of a column?
I check if the datarow is not DBNULL.Value before a convert operation to decimal.
I do check if the coloumn exists among the coloumns of the datarow.
What I do look for?
Are there any util class to manuplate the datasets, datatables to get row or an attribute's data? Or please tell me the possible check list over datasets, datatables, datarows to be always sure about no convertion error, no such an error linking with a dataset and its child objects.
Thank you!
If you want to check if a column exists in the datatable you can simply do this:
YourDataTable.Columns.Contains("column")
If you want to check if the value of a target row is null then I would do like this:
if(!Convert.IsDBNull(YourDataTable.Rows[0]["column"]))
{
//Something
}
If you get a dataset back you proboly also want to check if there is a DataTable
If(YourDataSet.Tables.Count>0)
{
//something
}
Depending on how may rows you are expecting. If you are expecting one row the you can do this:
if(YourDataTable.Rows.Count>0)
{
//something
}
I'm trying to edit the data in a dataset (change the value in a column on one row), which is not linked to a database. I've been googling for about an hour with no results and no good examples out there. Hopefully someone can help me.
My table (DataTable1) has these columns - ThreadID (string, PK), StatusText (string).
I can select a row in a DataGridView, and get the ThreadID value. No matter how I've tried to edit the row in the associated dataset, either nothing happens or I get an error. Here's what I have now:
string sThreadID = "";
sThreadID = gridThreads.Rows[gridThreads.CurrentRow.Index].Cells["ID"].Value.ToString(); // gives me a good id, which is in the dataset
DataRow drRow = dataThreads.Tables["DataTable1"].Rows.Find(sThreadID);
drRow["StatusText"] = "Test";
The error I get when getting the row (3rd line) is: "Object reference not set to an instance of an object.". I can't create a new DataRow object because there's no public constructor for it (according to my research).
I'm sure I'm missing something basic, but I'm not familiar with working with datasets. What am I doing wrong? Thanks for your help.
How does drRow know what "statusText" is? You may be only getting a shalllow copy, try cloning/copy so that the data types of the row get copied as well. Just an idea.
Does it happen that dataThreads or Tables["DataTable1"] is null ?
To create a new row, use the NewRow() method of the data table.
----> I have datatable which is passing to another page in session variable.
----> Now on another page i take the session variable into datatable.
datatable ds_table = new datatable();
ds_table = (datatable)session["table_value"];
----> so problem, is that , when i filtering some rows from ds_table . that taking effect in the session variable. if some rows deleted from ds_table. then it is also deleted from session variable.
----> so, anyone tell me why is this going to happene?
help me. its necessary.
I would suggest that you copy the datatable before placing it in session. Then, it won't be affected by any changes you make later to the original datatable.
All you have to do is declare a new DataTable variable and copy it:
private void CopyDataTable(DataTable table){
// Create an object variable for the copy.
DataTable copyDataTable;
copyDataTable = table.Copy();
// Insert code to work with the copy.
}
Then you can put copyDataTable into session.
Well, if session["table_value"] points to the datatable, and you make assign its value (the datatable) to another variable, and THEN MAKE CHANGES to that variable, the changes will be relfected in the datatable and thus you'll see the effect you illustrated.
----- Not always, but in the case you illustrated (so is datatable a shared resource?)
Sounds like you want to make a copy of the DataTable. Try this:
ds_table = ((datatable)session["table_value"]).Copy();
This will be bad if the DataTable is large, so bear that in mind. That said using Session for a large DataTable sounds like a bad idea anyway!
Of course whether that works or not depends on where you are changing its state (I've made some big assumptions). Perhaps describe in more detail what you are doing, and you will get more help (e.g. a code example).