c# - Getting results from a database query in SQL and displaying them in a datagrid view -


my problem dont know enought linking sql databases c# code im trying send search query sql database , display results onto data grid view. problem lay within returning , displaying data. below search method created in search button click event method:

// takes 1 of parameters , searches data base rows match parameter , argument private void searchbtn_click(object sender, eventargs e) {     switch(searchcb.selectedindex)     {         case 0:             selecteditem = "haulername";             break;         case 1:             selecteditem = "ticketnumber";             break;         case 2:             selecteditem = "leasename";             break;         case 3:             selecteditem = "ticketdate";             break;         case 4:             selecteditem = "customername";             break;         case 5:              selecteditem = "leaseoperator";             break;         case 6:             selecteditem = "productname";             break;     }     if (selecteditem.equals("ticketdate") == false)     {         query = "select * disposaldata.dbo.thirdpartydisposal " + selecteditem + " '" + searchtb.text.tostring() + "%';";         var connection = new sqlconnection(connectionstring);          connection.open();         sqlcommand command = new sqlcommand(query, connection);         sqldatareader reader = command.executereader();      }   } 

ive run debugger , works fine , executes cant life of me figure out how put results in data grid view table. , explanation possible great, bunch!

you should read sql injection. code work replace this

sqldatareader reader = command.executereader(); 

with

 sqlcommand cmd = new sqlcommand(query, con);   cmd.commandtype = commandtype.text;   sqldataadapter da = new sqldataadapter(cmd);   dataset ds = new dataset();   da.fill(ds, "ss");   datagridview1.datasource = ds.tables["ss"]; 

if using webforms add line, in winforms ignore it

 datagridview1.databind(); 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -