GetData() does not exist in the current context - c#

So I was trying follow along some asp.net tutorials on filling a Gridview with ajax.
On Microsoft's msdn example it has
DataSet ds = GetData(queryString);
which I found here.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basedataboundcontrol.datasource.aspx
they are including
<%# import namespace="System.Data" %>
<%# import namespace="System.Data.SqlClient" %>
and my C# codebehind already has
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
On this example here he is using GetData() as well.
http://www.aspsnippets.com/Articles/GridView---Add-Edit-Update-Delete-and-Paging-the-AJAX-way.aspx
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
But anyway I am getting the error
GetData() does not exist in the current context
when I try it in my C# codebehind
SqlCommand sql = new SqlCommand(command);
AddressContactSource.SelectCommandType = SqlDataSourceCommandType.Text;
AddressContactSource.SelectCommand = command;
DataSet ds= new DataSet;
ds= GetData(sql);
So what am I missing?

A GetData() method could be
DataSet GetData(String queryString)
{
// Retrieve the connection string stored in the Web.config file.
String connectionString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;
DataSet ds = new DataSet();
try
{
// Connect to the database and run the query.
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
// Fill the DataSet.
adapter.Fill(ds);
}
catch(Exception ex)
{
// The connection failed. Display an error message.
Message.Text = "Unable to connect to the database.";
}
return ds;
}
Its standard procedure:
1 You provide the sql queryString to the function
2 connect to your database
3 create and fill a DataSet with the result of the query and return the DataSet. Then assign the DataSet to the DataSource.
You have to implement functions like GetData(yourQueryString) yourself.
As connection string you take the string to your database (here are some examples: connectionstrings).
(Note: The above code example GetData() is just copied from the link you provided.)

Related

How to show data in a datagridview in oracle using c#

I am trying to to execute stored procedure that I have created in my oracle and trying to show data from stored procedure into datagridview using C#.Someone please help me how do i execute stored procedure.
It is actually pretty simple. You just call the stored procedure, use a DataAdapter and load/populate a DataTable from the adapter and finally bind the DataSource property of the DataGridView or similar control to the DataTable.
Here's a sample code :
using (var conn = new OracleConnection(connectionString))
using (var cmd = new OracleCommand("ProcedureName", conn) {
CommandType = CommandType.StoredProcedure }) {
conn.Open();
using(OracleDataAdapter da = new OracleDataAdapter (cmd))
{
DataTable dataTable = new DataTable();
da.Fill(dataTable);
dataGridView1.DataSource = dataTable;
}
conn.Close();
I personally never used Oracle, the code above is actually the general-ish code for such tasks. I hope this works.

Version exception error when connect to database using C# and SQL Server 2014

The connection to database is working if I am using SqlDataSource object but it's not working when I am using C# code.
See the error:
http://i.stack.imgur.com/FTLvn.jpg
The problem is the data set returning null while i can track the query on SQL Server profiler.
and i test the query on SQL query editor it returning a result can you told me what the wrong on the code:
Code to return result on Data Set
Code Sample:
static public DataSet GetData(string SqlStatement)
{
DataSet datasetdata=new DataSet();
using (SqlConnection connection =
new SqlConnection(ConfigurationManager.ConnectionStrings["Undp_PortalConnectionString1"].ToString()))
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(SqlStatement, connection);
adapter.Fill(datasetdata);
return datasetdata;
}
}
SqlStatement = Select * From Portal_PersonalInfo Where email = 'waleed.obyed#undp.org'
Table :
http://i.stack.imgur.com/jQMnX.jpg
You need to check this while the connection is OPEN - by default, the SqlDataAdapter will open and close the connection as needed - you never get a chance to check while it's open.
If you open the connection yourself
connection.Open();
before the call to .Fill() - then you will see the ServerVersion is quite nicely filled on the connection property:
I found the problem from the DataSet file on the App_Code called DataSet.xsd this file add to datasetdata Table called ES_ICTCR see the photo, i delete this file i think because it has common name DataSet.xsd.
check the Table Count= 2.
http://i.stack.imgur.com/f66JJ.jpg

Display data from SQL into chart via WCF service

I'm doing an assignment in inserting and displaying data from SQL into a chart (using Web Form or Highchart) via WCF service. I was able to get the data from SQL to WCF service but I don't know how to display it into a chart. I know using Web Form I can directly bind the data from SQL to the chart but the purpose of the assignment is to display the data via WCF service. Here are my code:
TempService:
public DataSet GetTemp()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "server=DUCPC\\SQLEXPRESS;database=Temprature;user=sa;password=123456";
SqlDataAdapter da = new SqlDataAdapter("select * from RoomTemp",con);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
Code to bind the data to the chart in Web Form:
protected void Page_Load(object sender, EventArgs e)
{
TempService.TempServiceClient tsc = new TempService.TempServiceClient();
DataSet ds = tsc.GetTemp();
TempChart.DataSource = ds;
TempChart.DataBind();
}
Of course the above code not working. Can anyone help to fix the code above? Is there a simple way to insert the data into the SQL database? Any help is appreciated :)

adding crystal report via code

i successfully designed and filled my Crystal report via code not via wizard.
i added Crystal report via addNEWITEM
i added dataset in aap_code via addNEWITEM
i added datatable into dataset via addNEWITEM
Via code i made report and filled dataset and table with data
Run and display. Successfully done.
but my question is that how to do it fully via code like for steps 1,2,3 ? I don't want to add it via AddNewItem etc, isn't there any way to add these via code ? i did, i created some datasets and table via code like we do for Gridview etc but that doesn't appear in DATA connections of crystal report etc.
String conStr =WebConfigurationManager.ConnectionStrings["LoginDatabaseConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Dataset_load();
}
}
protected void Dataset_load()
{
SqlConnection sqlcon = new SqlConnection(conStr);
SqlCommand sqlCom = new SqlCommand("select * from Login", sqlcon);
SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom);
// DataSet ds = new DataSet("CRDataSet");
try
{
sqlcon.Open();
//sqlCom.ExecuteNonQuery();
//sqlDA.Fill(ds,"Login");
DataSet1 ds = new DataSet1();
DataTable dt = new DataTable("DT_CR");
sqlDA.Fill(dt);
ds.Tables[0].Merge(dt);
ReportDocument rd = new ReportDocument();
rd.Load(Server.MapPath("CrystalReport.rpt"));
rd.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rd;
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
finally
{
sqlcon.Close();
}
I'm not sure why you would make it harder on yourself by trying to create all the code from scratch rather than using the functionality of VS. If you really want to do it though, I would create everything using the built-in functionality. Then I would look at all the code that gets added and see what does what.
There are some tutorials out there that tell you how to do it using the features. Create one and then read through the code.
That's how I would do it.
Hope that helps,
Chris

read data from ODBC using DataSet

I have created ODBS user DNS for database, opened VS, created DataSet and imported one table members. I would like to read all records from dataset, how to do that? I have tried query below but it return no result. I can preview data using preview menu in designer but do not find a way to get data using code.
var dataSet = new DataSet1();
var membersDataTable = dataSet.members;
var take = membersDataTable.Take(100);
It looks like you have created the schema for a DataSet, but you have not run any queries to load the DataSet.
using (OdbcConnection connection =
new OdbcConnection(connectionString))
{
string queryString = "SELECT * FROM Members";
OdbcDataAdapter adapter =
new OdbcDataAdapter(queryString, connection);
// Open the connection and fill the DataSet.
try
{
connection.Open();
adapter.Fill(dataSet);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.

Categories