-4

string declared inside class MyClass(TransactionId And pnrno) is not working in button click

ERROR MESSAGE

the name (TransactionId And pnrno) does not exsist in current context

class MyClass
{   
        
    public string TransactionID { get; set; }
    public string pnrno { get; set; }
    public string trainno { get; set; }
    public string dateofbooking { get; set; }
    public string className { get; set; }
    public string Quota { get; set; }
    public string text { get; set; }
}

protected void btnsave_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=logistics.jayom.org,1434;Initial Catalog=logistics_kl;User ID=kl_admin;Password=Admin@2222");
    SqlCommand cmd = new SqlCommand("insert into testfbhv (TransactionId, pnrno) values('" + TransactionID + "','" + pnrno + "')", con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}
Community
  • 1
  • 1
Ibibo
  • 167
  • 1
  • 4
  • 21

1 Answers1

6

btnsave_Click will be part of say Form1 class, not part of your MyClass you've shown. As a result, no it cant see the string you mentioned.

You would need to have created an instance of MyClass, and set some values to it.. (not mentioned in your example code) so then you might be able to..

Lets assume that Form1 has a variable of MyClass called mc - and is made on creation or such: then you your button click you can have mc.TransactionalID

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • Think of it as human terms. Let's say you're the form with the button click on it.. You know what you're thinking about all the things you have.. Im "MyClass" you cant tell what Im thinking without asking me. Classes are like people they are independent and you cant see something that a) you dont have permission to b) you cant call "transactionid" cos you dont have one, but I do.. You need to ask me for mine.. But also, you need me to exist before you can ask me, non static classes are just templates, so you need to have made an instance of me (cookie cutter) so you can talk to me. – BugFinder Jun 27 '16 at 10:43
  • I am very new in c# how to make an instance of a class – Ibibo Jun 27 '16 at 10:45
  • @Ibibo I mentioned in my comment below your question. You should create an instance of your class first like this `MyClass cs = new MyClass();` Then you can use it in your `insert` command like this: `cs.TransactionID`. – Salah Akbari Jun 27 '16 at 10:55
  • i used instance but forgetted thank you guys for your support – Ibibo Jun 27 '16 at 11:11