Reputation: 3360
I have got a couple of questions. I am making gym management system to submit in my university. I implemented a service based database by reading the tutorials at www.homeandlearn.co.uk. The problem is I can not update it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Gym_Management_System
{
class GMSDConnectionClass
{
public string sql_string;
public string strCon;
public string Sql
{
set { sql_string = value; }
}
public string connection_string
{
set { strCon = value; }
}
public System.Data.DataSet GetConnection
{
get { return MyDataSet(); }
}
public System.Data.DataSet MyDataSet()
{
System.Data.SqlClient.SqlConnection con = new
System.Data.SqlClient.SqlConnection(strCon);
con.Open();
System.Data.SqlClient.SqlDataAdapter da_1 = new
System.Data.SqlClient.SqlDataAdapter(sql_string, con);
System.Data.DataSet dat_set = new System.Data.DataSet();
da_1.Fill(dat_set, "Table_data_1");
con.Close();
return dat_set;
}
public void UpdateDatabase(System.Data.DataSet ds)
{
System.Data.SqlClient.SqlCommandBuilder cb = new
System.Data.SqlClient.SqlCommandBuilder(da_1);
cb.DataAdapter.Update(ds.Tables[0]);
}
}
}
The problem is that I can not access da_1
in UpdateDatabase()
method.
What's wrong there? I have tried couple of things but I couldn't get it. It may be a simple problem but I am totally new to database and this stuff.
Note: I have to submit this project on 23rd January 2014.
Upvotes: 1
Views: 134
Reputation: 65049
Variables cannot be accessed outside of their declaring scope. There are various scoping rules.. the one you're most concerned with here is method-scope. This might help explain:
public System.Data.DataSet MyDataSet()
{
// da_1 is delcared in this method.. it is only available here
...
System.Data.SqlClient.SqlDataAdapter da_1 = new
System.Data.SqlClient.SqlDataAdapter(sql_string, con);
...
}
public void UpdateDatabase(System.Data.DataSet ds)
{
// not available here
}
In order to access it across methods within a class.. you must declare it at class level:
class GMSDConnectionClass
{
System.Data.SqlClient.SqlDataAdapter da_1;
Then you can assign it like this in your other method:
da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
Upvotes: 1