sql Parameters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a DbManager class that has all my connect stuff like connecting to the
bd and inserting data, but for some reason when I call the insert coammand
for the DbManager class I get A Method 'Network_Inventory.DbManager.Insert()'
referenced without parentheses

Here is my DbManager Class code
public void Insert()
{
OleDbCommand cmdInsert = new OleDbCommand();
cmdInsert.ExecuteNonQuery();
}
Here is my save btn that calls it
private void btnSave_Click(object sender, System.EventArgs e)
{
DbManager manager = new DbManager();
manager.conn();
manager.Insert = "insert into Wired(RouterName) values ('" +
txtRouterName.Text + "')";
}
How would I change
private void btnSave_Click(object sender, System.EventArgs e)
{
DbManager manager = new DbManager();
manager.conn();
manager.Insert = "insert into Wired(RouterName) values ('" +
txtRouterName.Text + "')";
}
to sql Parameters
 
freddy said:
I have a DbManager class that has all my connect stuff like connecting to the
bd and inserting data, but for some reason when I call the insert coammand
for the DbManager class I get A Method 'Network_Inventory.DbManager.Insert()'
referenced without parentheses

And it's absolutely right - you've used it as if it were a property:
manager.Insert = "insert into Wired(RouterName) values ('" +
txtRouterName.Text + "')";

Note that inserting values directly into SQL is very dangerous - it
opens you up to SQL injection attacks. You should use parameterised
queries instead.
 
Freddy,

I don't see for what kind of database (I assume something for CE) you are
making this, however I think that this code can work, where I don't
understand the Insert String and assume that you have created the connection
in the constructor from the Class DBManager. The connection close is not
done in this by the way, I don't know how your database act on that.

public void Insert(string ins)
{
OleDbCommand cmdInsert = new OleDbCommand(ins);
cmdInsert.ExecuteNonQuery();
}

private void btnSave_Click(object sender, System.EventArgs e)
{
DbManager manager = new DbManager();
manager.Insert("insert into Wired(RouterName) values ('" +
txtRouterName.Text + "')";
}

Just rougly changed in your message so watch typos or others mistakes.

I hope this helps?

Cor
 
Back
Top