Click here to Skip to main content
15,063,287 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to csharp, I cannot update the dataadapter , Here is the code
C#
namespace app1
{
    public partial class Form1 : Form
    {
        MySql.Data.MySqlClient.MySqlConnection conn;
     
       public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {     }

        private void radPageViewPage1_Paint(object sender, PaintEventArgs e)
        {
        }

        public void radButton2_Click(object sender, EventArgs e)
        {

            string myConnectionString;
          
            myConnectionString = "server=localhost;uid=root;" +
                "pwd=xxxx;database=ldis;";
            
            conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);

                 conn.Open();

            DataTable dtusers = new DataTable() ;
             dtusers = Getusers();
             radGridView2.DataSource = dtusers;
               conn.Close();
             
        }



       private DataTable Getusers()
        {

        string query = "select userid,password,user_group,status,expiry from users";
        MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn); // adapter declaration
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            
            adapter.UpdateCommand = new MySqlCommand(
            "UPDATE users SET  password=@password, user_group=@user_group,status=@status  WHERE userid=@userid;", conn);
            adapter.UpdateCommand.Parameters.Add("@userid", MySqlDbType.VarChar, 25, "userrid");
            adapter.UpdateCommand.Parameters.Add("@password", MySqlDbType.VarChar, 45, "password");
            adapter.UpdateCommand.Parameters.Add("@user_group", MySqlDbType.VarChar, 45, "user_group");
            adapter.UpdateCommand.Parameters.Add("@status", MySqlDbType.VarChar, 10, "status");      
            adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;

            return ds.Tables[0];

                      

        }


       public void save()
       {
          
       }

        private void button1_Click(object sender, EventArgs e)
        {
         
        }

        private void radButton1_Click(object sender, EventArgs e)
        {

            adapter.update();

            // The name adapter does not exists in the current context
        }



    }
}



I am able to retrieve the data.
but when I want to add the command adapter.update() in other buttons event, I get this error called ,
The name adapter does not exists in current context.

Checked lots of examples, but no one seems to get this current context errors.

Thanks in advance
Omer
Posted
Updated 14-Dec-11 10:55am
v2

1 solution

The adapter is defined in your method Getusers so it's not visible to other methods. Consider defining the adapter as class variable along with the conn. Something like:
C#
public partial class Form1 : Form
    {
        MySql.Data.MySqlClient.MySqlConnection conn;
        MySqlDataAdapter adapter;
...
private DataTable Getusers()
        {
 
        string query = "select userid,password,user_group,status,expiry from users";
        adapter = new MySqlDataAdapter(query, conn); 
...

Also read more about scopes, for example: http://www.blackwasp.co.uk/CSharpVariableScopes.aspx[^]
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900