James Timothy
James Timothy

Reputation: 11

c# property won't work?

hello guys i have a form... and i set my properties if the user will click the submit button and after then i will call my add_data function which contains my database query...but the problem is the properties I've set in my form will become empty in my add_data function...why this is happening?

actually i already try adding a messagebox in my form which contains the data in my properties after setting my properties value and it works fine but when i add it to my databasecon class the messagebox shows null... i try also putting my properties and database query function in the same class and it's working but what i want is to separate my properties and my database query functions...

this is the codes in my properties

class persons
{
    //person attributes
    private string fname;
    private string lname;
    private string age;
    private string gnder;
    private string address;
    //initialize
    public persons()
    {
        this.fname = "";
        this.lname = "";
        this.age = "";
        this.gnder = "";
        this.address = "";
    }
    //set and get properties
    public string p_fname
    {
        get { return this.fname; }
        set { this.fname = value; }
    }
    public string p_lname
    {
        get { return this.lname; }
        set { this.lname = value; }
    }
    public string p_age
    {
        get { return this.age; }
        set { this.age = value; }
    }
    public string p_gender
    {
        get { return this.gnder; }
        set { this.gnder = value; }
    }
    public string p_address
    {
        get { return this.address; }
        set { this.address = value; }
    }
}

this is the codes in my form

public partial class add : Form
{
    persons p = new persons();
    databasecon d = new databasecon();
    private void addbtn_Click(object sender, EventArgs e)
    {
        p.p_fname = this.fname.Text;
        p.p_lname = this.lname.Text;
        p.p_age = this.age.Text;
        p.p_gender = this.gender.Text;
        p.p_address = this.address.Text;
        d.add_data();
        this.Close();
    }
}

and this is the codes in my database connection and queries

class databasecon
{
    OleDbConnection con = new OleDbConnection();
    persons p = new persons();
    public databasecon()
    {
        con.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=../dbsample.mdb";
    }

    public void add_data()
    {
        try
        {
            con.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO person(u_fname,u_lname,u_age,u_gender,u_address)VALUES('" + p.p_fname + "','" + p.p_lname + "','" + p.p_age + "','" + p.p_gender + "','" + p.p_address + "')";
            cmd.Parameters.AddWithValue("u_fname", p.p_fname);
            cmd.Parameters.AddWithValue("u_lname", p.p_lname);
            cmd.Parameters.AddWithValue("u_age", p.p_age);
            cmd.Parameters.AddWithValue("u_gender", p.p_gender);
            cmd.Parameters.AddWithValue("u_address", p.p_address);
            cmd.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            MessageBox.Show("Error : " + e);
        }
        finally
        {
            con.Close();
            MessageBox.Show("New person has been successfully added.");
        }
    }
}

Upvotes: 1

Views: 235

Answers (9)

Salvatore Previti
Salvatore Previti

Reputation: 9080

There are several things I don't like in your code.

Let's start however with your specific problem:

Your code contains also a source of security issues and malignous SQL code injection.

You are saving always an empty person and there is a problem with your SQL connection string.

Try this code instead.

private void addbtn_Click(object sender, EventArgs e)
{
    try
    {
        persons p = new persons(); // we use a new instance of person class
        p.p_fname = this.fname.Text;
        p.p_lname = this.lname.Text;
        p.p_age = this.age.Text;
        p.p_gender = this.gender.Text;
        p.p_address = this.address.Text;
        d.add_data(p); // we save the instance of persons
        this.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error : " + e);
    }
}

...

class databasecon
{

    public void add_data(person p) // we need a person as parameter
    {
        OleDbConnection con = new OleDbConnection();
        con.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=../dbsample.mdb";
        con.Open();
        try
        {
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            // this is the correct sql command string
            cmd.CommandText = "INSERT INTO person(u_fname,u_lname,u_age,u_gender,u_address) " +
                VALUES (@u_fname, @u_lname, @u_age, @u_gender, @u_address)";
            cmd.Parameters.AddWithValue("u_fname", p.p_fname);
            cmd.Parameters.AddWithValue("u_lname", p.p_lname);
            cmd.Parameters.AddWithValue("u_age", p.p_age);
            cmd.Parameters.AddWithValue("u_gender", p.p_gender);
            cmd.Parameters.AddWithValue("u_address", p.p_address);
            cmd.ExecuteNonQuery();
        }
        finally
        {
            con.Close();
        }
    }

    ...
}

Now let's talk about code style.

Is a good thing to use CamelCase style in your code, look on the web about C# CamelCase: Classes and properties should all start with a capitalized letter.

Your class express a single person not a list of persons so its name should be public class Person.

Avoid the use achronims or of short names when you can... p_lname should be LastName, people will thanks you if you make your code more readable, C# is not C and C# is not PHP!

A field or a property with a longer name will not consume more memory than a property with a short and unreadable name :)

Use always strong typing... age is not a string, age is an integer. The property should be public int Age, not a string!

Never use MessageBox in your non-visual classes. Try to catch for exceptions in your client code, not in your library code!

Upvotes: 1

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4908

you must pass person object to add_data method

public void add_data(persons p)
{
   ...
}

and call it:

d.add_data(p);

Upvotes: 0

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25475

I dont know if I 100% understand the issue about I guess it's because your inserting an empty persons class into the db.

Your `add_data method should take a person object like so.

public void add_data(persons obj)
{
    p = obj;

Upvotes: 0

RLH
RLH

Reputation: 15698

From what I can see in the code above, you call add_data from your databasecon class, which has an instance p of Person. Since you are not passing a Person object to your add_data method, the empty, unset p object is what is being saved to the database.

Adding a Person parameter to add_data and use that when saving the data to the database.

Upvotes: 1

Richard Friend
Richard Friend

Reputation: 16018

Overlooking the fact that your code is extremely poorly written.

You are not passing in the persons class that you created.

Something like

public void add_data(persons p) {//..blah}

Then

private void addbtn_Click(object sender, EventArgs e)      {          
    p.p_fname = this.fname.Text;
    p.p_lname = this.lname.Text;          
    p.p_age = this.age.Text;          
    p.p_gender = this.gender.Text;          
    p.p_address = this.address.Text;          
    d.add_data(p);         
    this.Close();      
 }  

Upvotes: 1

Beku
Beku

Reputation: 405

You have an instance of Person class which you fill and then use an instance of databasecon which is completely not conntected to the person class you filled. Change add_data() to

public void add_data(person p) { ... }

this will use the properties from p passed as parameter.

You call it like this

d.add_data(p); 

Except for that have a look at some C# for begginers book.

Upvotes: 1

Krisc
Krisc

Reputation: 1427

Your p fields in your add and databasecon classes are separate. When you call d.add_data(), the d object can only see its instance of persons p ....

To fix this, pass the persons object into the add_data method.

class databasecon{
    // Remove this line, we pass it into the function below
    /* persons p = new persons(); */

    public void add_data(persons p){
        try{
            // same code as before
        }catch(Exception e){
            // same code
        }
        finally{
            // same
        }
    }
}

Upvotes: 1

wiero
wiero

Reputation: 2246

you create databasecon() in form and then call add_data method and you don't pass 'persons' instance. in databasecon you use persons istnace which is field in this class. you soudl add parameter do add_data method and pass instance or 'persons' you want to save and use it in command

Upvotes: 1

CyberDude
CyberDude

Reputation: 8989

You need to pass p as a parameter to the add_data method.

public void add_data(persons person)

then call it with the parameter:

d.add_data(p);

and use the properties of person in the method:

cmd.Parameters.AddWithValue("u_fname", person.p_fname);
cmd.Parameters.AddWithValue("u_lname", person.p_lname);
cmd.Parameters.AddWithValue("u_age", person.p_age);
cmd.Parameters.AddWithValue("u_gender", person.p_gender);
cmd.Parameters.AddWithValue("u_address", person.p_address);

Upvotes: 4

Related Questions