private void import()
{
DataSet ds = new DataSet();
Export(ds);
}
[WebMethod]
public void Export(DataSet ds)
{
DataSet Ip_ds = new DataSet();
Ip_ds = ds; // insert this data in table
}
You dont need to pass a whole dataset as a parameter in the url .you can just pass the unique identifiers of the dataset as a parameter in the url and then use that identifier to get the required values.
Use HTTP POST method to post the data(Dataset) to the URL. You may need to use Newtonsoft JSON for serializing the Dataset.
Related
I have an old application that I wrote in Access VBA, the time has come to upgrade the code and the company decided to go with C# since we use it the most. My question is following, I have this code in VBA that works great,
Set RS2 = Db.OpenRecordset("Select * FROM TTable WHERE ID="&Forms![test]![SifraFirme]&")
su = RS2.RecordCount
RS2.MoveFirst
Do While Not RS2.EOF
//lines of code
RS3.MoveNext
Loop
RS3.Close
Now my question is, is there a C# command similar to Do While Not RS.EOF, any literature or examples would be highly appreciated. Just a nudge in the right direction because it has become frustrating. The main point of code above is to go through the table and filter the data and write it to XML (predefined structure) based on ID once he is done with first, move on to the second, and ...
Thank you,
Answering to:
The main point of code above is to go through the table and filter the
data and write it to XML
You can read database table to some DataSet, using OleDbDataAdapter from System.Data namespace. Then easily work with filled DataSet or instantly get its XML representation by GetXml method:
static void Main(string[] args)
{
// Note about set Prefer 32-bit app version of your C# app to use Jet.OLEDB provider
var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourDBPath";
var query = "Select * FROM TTable";
// Introducing our DataSet
var dataSet = new System.Data.DataSet();
using (var connection = new System.Data.OleDb.OleDbConnection(connectionString))
{
var command = new System.Data.OleDb.OleDbCommand(query, connection);
try
{
connection.Open();
using (var dataAdapter = new System.Data.OleDb.OleDbDataAdapter(command))
{
// Fill DataSet
dataAdapter.Fill(dataSet);
}
// Get XML representation of DataSet and save to XML file
System.IO.File.WriteAllText(#"TTable.xml", dataSet.GetXml());
// Or if need to filter data before save - read through DataSet
var TTable = dataSet.Tables["TTable"];
foreach (var row in TTable.Rows.Cast<System.Data.DataRow>().ToArray()) // using System.Linq needed
{
}
}
catch (System.Exception ex)
{
// Handle exception in some way
System.Console.WriteLine(ex.Message);
}
}
System.Console.ReadKey();
}
C# has the XMLWriter class and you can use the SQL classes for querying and reading the information.
The while loop in C# would be something like this:
while (!RS2.EOF)
{
//lines of code
RS2.MoveNext();
}
The ! is the Logical negation operator.
ADO.NET has the DataSet class which works with data in a way that is similar to a RecordSet in VBA.
See Microsoft's documentation on DataSet
i need to be able to save and retrieve data in a dataset in multiple places. i didnt want to copy and paste all my code since its pretty large, and i hope my super short version gets the idea across. thanks in advance. i currently have something along the lines of...
class program
{
DataSet ds;
static void main(...)
{
getMe(string);
}
public void getMe(string x)
{
ds = new mydataset();
DataRow dr = new ds.Tables[0].NewRow();
//blah blah add x to dr[ column ]
ds.Tables[0].Rows.Add(dr.ItemArray);
}
public void readMe()
{
**need to read dataset here with info added in rows from getMe()
}
}
EDIT:
edited DataSet = ds; to DataSet ds; to reflect my actual code.
if i call getMe(string) in my main, it errors with..."an object reference is required for a non-static field, method or property."
if i change getMe(string x) to public static void getMe(string) the error goes away but shows again for
ds = new mydataset();
I have a datatable containing file paths which I am passing via viewstate (referencing, via a linkbutton, an index in this table), wanting to then use the path from the table to construct a HTTP filetransfer. (So 3 cols; name, path and index)
I am unable to successfully retrieve the datatable once saved in viewstate;
ViewState["varFiles"] = filedata;
(When page is originally constructed, then after postback:)
if (!IsPostBack) { SetupSession(); newpopfiles(); }
else { { if (ViewState["varFiles"] != null) { DataTable filedata = new DataTable(); filedata = (DataTable)Session["varFiles"]; } } }
From what I understand this should pull back filedata as a table in exactly the same form as before postback. Is this correct?
When subsequently referencing the table I get a null reference exception. Any ideas?
Many thanks,
Dan
It sounds like you're almost there, just need to be a bit more consistent with using the same storage mechanism :)
The bit to save the DataTable into your session, probably in OnInit() or PageLoad():
DataTable myDataTable = //... fill it in somehow
Session["varFiles"] = myDataTable;
The bit to read the DataTable after postback:
if (!IsPostBack)
{
SetupSession();
newpopfiles();
}
else
{
DataTable filedata = Session["varFiles"] as DataTable;
if (filedata != null)
{
//... do something
}
}
I am trying to load my dynamically generated Datatable during runtime into JS array in javascript. But cannot pass it through webservice as only static methods can be called through javascript.How to return the datatable into javascript.
aspx.cs file :
protected void btnRead_Click(object sender, EventArgs e)
{
//on button click it reads file path of excel and stores it in a string path
}
private DataTable ReadExcelWithStream(string path)
{
//method reads the excel file and stores it in a dt
ForJs(dt);
return dt;
}
[ScriptMethod, WebMethod]
public static DataTable ForJs(DataTable dt)
{
return dt;
}
aspx file :
<script type="text/javascript">
function InsertLabelData() {
PageMethods.ForJs(onSuccess, onFailure);
}
function onSuccess(dt) {
//attach the table to dhtmlx grid
}
But the datatable is not passed to the javascript.
How to pass the datatable to javascript from c# ?
or any other methods to pass the datatable from c# to JS
Please Help.
Thanks!
duplicate?/related question: Dynamically pass datatable from C# to Javascript
You can not simply pass an(y) object to JavaScript. It has to be in a usable format. Like an array or serialized to XML or JSON. Also "DataTables contain lots of additional information which JSON cannot store".
Take a look at how to pass c# datatable to a javascript function or What's the best way to jSON serialize a .NET DataTable in WCF? for more information on this subject.
I am a student programer working in Visual Studios C# and I am trying to access information from my dataset and insert the data into a class. This is not, by any means, homework I've just have some personal projects I've been wanting to try. I have tried several approaches that have been mentioned on this site; however, no information is displayed. My code looks similar to this:
class MyClass
{
public string ColumnData1
{
get; set;
}
public int ColumnData2
{
get; set;
}
public string Display()
{
string MyString = ColumnData1 + ColumnData2.ToString();
return MyString;
}
}
I use this to insert data into class:
private void MyForm_Load(object sender, EventArgs e)
{
MyDataSet.MyDataTable MDT = new MyDataSet.MyDataTable();
List<MyClass> MyList = new List<MyClass>();
foreach (DataRow MyDataRow in MDT.Rows)
{
Mylist.Add(new MyClass()
{
ColumnData1 = (string)MyDataRow["Data1"],
ColumnData2 = (int)MyDataRow["Data2"]
{
}
Lastly to display the information:
textBox1.Text = Mylist[0].Display();
}
In the end, however, nothing ends up displaying. This also wasn't the only thing I've tried to display the information.. it's like the information doesn't exist. I don't receive any errors and when I try to add a "Stop Point" at the insertion part of the code it just skips it. I should mention also that I have many text boxes and list boxes that pull data off the database just fine, of course Visual Studios binds those for me. Any help is very much appreciated.
Edit:
Ok, excluding the new data table. If I had an existing dataset how would I would I use it to fill my class.
That's normal because your DataTable is empty (Your create instance)
MyDataSet.MyDataTable MDT = new MyDataSet.MyDataTable();//<------Empty
foreach (DataRow MyDataRow in MDT.Rows)
{
....
}
Fill DataSet :
string queryString =
"SELECT .... FROM YourTable";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet, "NameOfDataSet");
You must have some data in MDT before you insert it in the MyList, List of MyClass.
Declaration without data:
MyDataSet.MyDataTable MDT = new MyDataSet.MyDataTable();