Click here to Skip to main content
15,901,666 members
Home / Discussions / C#
   

C#

 
GeneralRe: Current datagrid row not being selected Pin
Member 1144944710-Dec-15 3:49
Member 1144944710-Dec-15 3:49 
GeneralRe: Current datagrid row not being selected Pin
Richard Deeming10-Dec-15 4:18
mveRichard Deeming10-Dec-15 4:18 
GeneralRe: Current datagrid row not being selected Pin
Member 1144944710-Dec-15 5:34
Member 1144944710-Dec-15 5:34 
GeneralRe: Current datagrid row not being selected Pin
Richard Deeming10-Dec-15 5:53
mveRichard Deeming10-Dec-15 5:53 
GeneralRe: Current datagrid row not being selected Pin
Member 1144944714-Dec-15 6:25
Member 1144944714-Dec-15 6:25 
GeneralRe: Current datagrid row not being selected Pin
Member 1144944714-Dec-15 7:08
Member 1144944714-Dec-15 7:08 
GeneralRe: Current datagrid row not being selected Pin
Eddy Vluggen10-Dec-15 4:43
professionalEddy Vluggen10-Dec-15 4:43 
Questionwhy do not SetFocus row for the GridView when using the Thread ? Pin
Member 24584679-Dec-15 21:20
Member 24584679-Dec-15 21:20 
AnswerRe: why do not SetFocus row for the GridView when using the Thread ? Pin
Simon_Whale9-Dec-15 21:58
Simon_Whale9-Dec-15 21:58 
GeneralC# ECG Analysis Algorithm Pin
Member 121973799-Dec-15 18:45
Member 121973799-Dec-15 18:45 
GeneralRe: C# ECG Analysis Algorithm Pin
Mycroft Holmes9-Dec-15 19:00
professionalMycroft Holmes9-Dec-15 19:00 
GeneralRe: C# ECG Analysis Algorithm Pin
Eddy Vluggen10-Dec-15 4:46
professionalEddy Vluggen10-Dec-15 4:46 
GeneralRe: C# ECG Analysis Algorithm Pin
Gerry Schmitz11-Dec-15 7:14
mveGerry Schmitz11-Dec-15 7:14 
GeneralHow I Can Make My Program With Multiple Language For Forms In C# ? Pin
eng_aza9-Dec-15 15:07
eng_aza9-Dec-15 15:07 
GeneralRe: How I Can Make My Program With Multiple Language For Forms In C# ? Pin
Mycroft Holmes9-Dec-15 19:03
professionalMycroft Holmes9-Dec-15 19:03 
GeneralRe: How I Can Make My Program With Multiple Language For Forms In C# ? Pin
Gerry Schmitz11-Dec-15 7:15
mveGerry Schmitz11-Dec-15 7:15 
QuestionSolid color image vs Panel with BackColor Pin
David Sattler9-Dec-15 5:34
David Sattler9-Dec-15 5:34 
AnswerRe: Solid color image vs Panel with BackColor Pin
OriginalGriff9-Dec-15 5:48
mveOriginalGriff9-Dec-15 5:48 
GeneralRe: Solid color image vs Panel with BackColor Pin
David Sattler9-Dec-15 5:52
David Sattler9-Dec-15 5:52 
Questionhow to fill the cells of a DataGridView directly Pin
Member 114494479-Dec-15 1:49
Member 114494479-Dec-15 1:49 
AnswerRe: how to fill the cells of a DataGridView directly Pin
Richard MacCutchan9-Dec-15 2:37
mveRichard MacCutchan9-Dec-15 2:37 
GeneralRe: how to fill the cells of a DataGridView directly Pin
Member 114494479-Dec-15 3:58
Member 114494479-Dec-15 3:58 
GeneralRe: how to fill the cells of a DataGridView directly Pin
Richard Deeming9-Dec-15 4:02
mveRichard Deeming9-Dec-15 4:02 
GeneralRe: how to fill the cells of a DataGridView directly Pin
Member 114494479-Dec-15 4:30
Member 114494479-Dec-15 4:30 
GeneralRe: how to fill the cells of a DataGridView directly Pin
Richard Deeming9-Dec-15 4:44
mveRichard Deeming9-Dec-15 4:44 
Member 11449447 wrote:
Which query is vulnerable to injection?

Take a guess!
Member 11449447 wrote:
string  Commandtext = "insert into lojas (NIF,loja,Bloqueado,DataFim,lastupdate,Nome) values ('" + NIF + "', " + loja + "," + bloqueador + ",'" + DataFim + "','" + lastupdate + "','" + Nome + "')";
OdbcCommand cm2 = new OdbcCommand(Commandtext, con);

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

The OdbcCommand uses positional, not named, parameters. You have to use ? as the parameter placeholder in the query, and add the parameters in the same order as they appear in the query.
C#
using (OdbcConnection connection = CreateConnection())
using (OdbcCommand command = new OdbcCommand("insert into lojas (NIF, Loja, bloqueado, DataFim, lastupdate, Nome) values (?, ?, ?, ?, ?, ?)", connection))
{
   command.CommandType = CommandType.Text;
 
   // Parameter names don't matter here; only the order:
   command.Parameters.AddWithValue("NIF", grid_lic.CurrentRow.Cells[1].Value);
   command.Parameters.AddWithValue("Loja", grid_lic.CurrentRow.Cells[2].Value);
   command.Parameters.AddWithValue("Bloqueado", checkBox_bloq.Checked);
   command.Parameters.AddWithValue("DataFim", grid_lic.CurrentRow.Cells[4].Value);
   command.Parameters.AddWithValue("lastupdate", grid_lic.CurrentRow.Cells[5].Value);
   command.Parameters.AddWithValue("Nome", grid_lic.CurrentRow.Cells[6].Value);
   
   connection.Open();
   command.ExecuteNonQuery();
}


As to your original problem: you're storing the OdbcConnection instance in a field, which is a bad idea. Your BD_Conexao method is creating and opening a new connection, but only stores it in a local variable. The field is never updated, so your bt_preencher_Click method is unable to use that field.

Remove the field, and use a method which creates and returns the connection object instead:
C#
private static OdbcConnection CreateConnection()
{
    return new OdbcConnection("driver= {MySQL ODBC 5.1 Driver};server=xxxxx; database=lic; uid=es; password=1234; option = 3 ");
}

public void Consulta()
{
    using (OdbcConnection connection = CreateConnection())
    using (OdbcCommand command = new OdbcCommand("SELECT Id, NIF, Loja, Bloqueado, DataFim, lastupdate, Nome FROM lojas", connection))
    {
        ...
    }
}

private void bt_preencher_Click(object sender, EventArgs e)
{
    using (OdbcConnection connection = CreateConnection())
    using (OdbcCommand command = new OdbcCommand("insert into lojas (NIF, Loja, bloqueado, DataFim, lastupdate, Nome) values (?, ?, ?, ?, ?, ?)", connection))
    {
        ...
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.