Click here to Skip to main content
15,887,453 members
Home / Discussions / C#
   

C#

 
QuestionOne button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Member 114494477-Jan-16 22:49
Member 114494477-Jan-16 22:49 
AnswerRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
OriginalGriff7-Jan-16 23:16
mveOriginalGriff7-Jan-16 23:16 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Member 114494478-Jan-16 1:08
Member 114494478-Jan-16 1:08 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
OriginalGriff8-Jan-16 1:22
mveOriginalGriff8-Jan-16 1:22 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Member 114494478-Jan-16 1:51
Member 114494478-Jan-16 1:51 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Richard MacCutchan8-Jan-16 2:53
mveRichard MacCutchan8-Jan-16 2:53 
GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Dave Kreskowiak8-Jan-16 3:58
mveDave Kreskowiak8-Jan-16 3:58 
SuggestionRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Richard Deeming8-Jan-16 2:08
mveRichard Deeming8-Jan-16 2:08 
Member 11449447 wrote:
string Cmd = "Select * from lojas where NIF ='" + grid_lic.CurrentRow.Cells[1].Value + "' and loja ='" + grid_lic.CurrentRow.Cells[2].Value + "'";
 
OdbcCommand cm2 = new OdbcCommand(Cmd, con);
OdbcDataReader dr = cm2.ExecuteReader();

Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Also, you should always wrap objects which implement IDisposable in a using block, to ensure that their resources are released properly.
C#
using (OdbcConnection con = new OdbcConnection("driver= {MySQL ODBC 5.1 Driver};server=xxx; database=licenciamento; uid=estagio; password=1234; option = 3 "))
{
    con.Open();
    
    using (OdbcCommand cm2 = new OdbcCommand("Select * from lojas where NIF = ? and loja = ?", con))
    {
        cm2.Parameters.AddWithValue("NIF", grid_lic.CurrentRow.Cells[1].Value);
        cm2.Parameters.AddWithValue("loja", grid_lic.CurrentRow.Cells[2].Value);
        
        using (OdbcDataReader dr = cm2.ExecuteReader())
        {
            // TODO: Read the results and do something with them...
        }
    }

    using (OdbcCommand cm = new OdbcCommand("insert into licenciamentoloja (bloqueador, EArtigo) values (?, ?)", con))
    {
        cm.Parameters.AddWithValue("@bloqueador", bloqueador);
        cm.Parameters.AddWithValue("@EArtigo", EArtigo);
        cm.ExecuteNonQuery();
    }
}


Member 11449447 wrote:
if (grid_lic.CurrentRow.Cells[3].Value.ToString() != "" | grid_lic.CurrentRow.Cells[3].Value.ToString() == "True")

You're using the non-short-circuiting boolean operator |. In the vast majority of cases (including this one), that's the wrong operator to use. You should use the short-circuiting operator || instead.
C#
if (grid_lic.CurrentRow.Cells[3].Value.ToString() != "" || grid_lic.CurrentRow.Cells[3].Value.ToString() == "True")

However, that still doesn't make any sense. If the value of the cell is not an empty string, then the first condition is true. If it is an empty string, then there's no way it can be equal to "True", so the second condition is false. The whole statement is equivalent to:
C#
if (grid_lic.CurrentRow.Cells[3].Value.ToString() != "")


Member 11449447 wrote:
public void BD_Conexao()

As Griff said, this method does nothing useful. It looks like you're trying to initialize and open a connection object stored in a class-level field called con, but you've declared a local variable of the same name instead.

Storing connection objects in fields is generally a bad idea. You should created and open the connection within the method that uses it, wrapped in a using block. Have a single method to create and open a new connection object, and return that connection:
C#
public OdbcConnection BD_Conexao()
{
    var result = new OdbcConnection("driver= {MySQL ODBC 5.1 Driver};server=xxx; database=licenciamento; uid=estagio; password=1234; option = 3 ");
    result.Open();
    return result;
}

Then call that method from your other methods when you need to connect to the database:
C#
using (OdbcConnection con = BD_Conexao())
{
    ...
}




Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
SQL injection attack mechanics | Pluralsight [^]




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


GeneralRe: One button from the toolstrip bar not updating checkboxes from the datagrid columns Pin
Member 114494478-Jan-16 6:04
Member 114494478-Jan-16 6:04 
Questionparse xml Pin
MaheshSharma7-Jan-16 16:33
MaheshSharma7-Jan-16 16:33 
SuggestionRe: parse xml Pin
dan!sh 7-Jan-16 16:48
professional dan!sh 7-Jan-16 16:48 
AnswerRe: parse xml Pin
Richard MacCutchan7-Jan-16 22:10
mveRichard MacCutchan7-Jan-16 22:10 
AnswerRe: parse xml Pin
BillWoodruff8-Jan-16 0:49
professionalBillWoodruff8-Jan-16 0:49 
QuestionC# swf decompiler Pin
Member 122071957-Jan-16 5:31
Member 122071957-Jan-16 5:31 
AnswerRe: C# swf decompiler Pin
OriginalGriff7-Jan-16 5:47
mveOriginalGriff7-Jan-16 5:47 
Questionstop a task Pin
caradri6-Jan-16 23:34
caradri6-Jan-16 23:34 
AnswerRe: stop a task Pin
Simon_Whale6-Jan-16 23:42
Simon_Whale6-Jan-16 23:42 
AnswerRe: stop a task Pin
Pete O'Hanlon7-Jan-16 0:04
mvePete O'Hanlon7-Jan-16 0:04 
GeneralRe: stop a task Pin
caradri7-Jan-16 1:42
caradri7-Jan-16 1:42 
GeneralRe: stop a task Pin
Pete O'Hanlon7-Jan-16 1:50
mvePete O'Hanlon7-Jan-16 1:50 
GeneralRe: stop a task Pin
caradri7-Jan-16 3:42
caradri7-Jan-16 3:42 
GeneralRe: stop a task Pin
Dave Kreskowiak7-Jan-16 3:46
mveDave Kreskowiak7-Jan-16 3:46 
QuestionWPF double click edit listview item label Pin
Ranjith Kumar5-Jan-16 20:30
Ranjith Kumar5-Jan-16 20:30 
AnswerRe: WPF double click edit listview item label Pin
dan!sh 6-Jan-16 0:32
professional dan!sh 6-Jan-16 0:32 
QuestionWay the open Windows freeze when I go back to open Windows Pin
Member 107434224-Jan-16 16:33
Member 107434224-Jan-16 16:33 

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.