c# - variable was undeclared or was never assigned? and data reader not working? -


i didn't want post couldn't find answer solve problem.

im making application friend, when run it, gives me warning, , started crashing.

i tried debugging problem, , gave me different problem.

class dbapplication : dbinfo {     private mysqlconnection connection = new mysqlconnection();     private int customer_id;     public string lastname;      //constructor     public dbapplication()     {         orderid();     }      public string orderid()     {         customer_id = 8;         //add database query string.         string query = "select * wdb_customer where=" + customer_id;          if (openconnection() == true)         {             mysqlcommand mycomm = connection.createcommand();             mysqldatareader reader;             mycomm.commandtext = query;             /*when debugged, said problem?                dont understand why.*/*             reader = mycomm.executereader();               while (reader.read())             {                 lastname = reader["customer_name"].tostring();             }             reader.close();         }         return lastname;     } } 

the first problem "the variable db_app either undeclared or never assigned."

thats script.

partial class form1 {     ...     private dbapplication db_app = new dbapplication();     ...     private void initializecomponent()     {        this.orderid.text = db_app.lastname;     } 

i have tried db_app.orderid(); same problem.

your query invalid since miss column name in where clause

to apply quick fix change

string query = "select * wdb_customer where=" + customer_id; 

to

string query = "select * wdb_customer customer_id = " + customer_id;                                                  ^^^ use real column name here 

on side note: not build sql query strings directly user input; use parameters instead. prevents code sql injections. read more here

this being said code might this

... mycomm.commandtext = "select * wdb_customer customer_id = @customer_id"; mycomm.parameters.addwithvalue("@customer_id", customer_id); reader = mycomm.executereader();  ... 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -