c# - Access database query string -


i have database 2 tables; studentid primary key on 'studentlist' table , studentid foreign key on 'journalentries' table.

on website have search function users can search student studentlist , results displayed in datagrid. when user selects student list journal entry box pops up. when fill out journal entry pop-up box , click submit button need journal entry entered journalentries table tied student id selected datagrid.

protected void searchgrid_selectedindexchanged(object sender, eventargs e) {     fnamelabel.text = searchgrid.selectedrow.cells[1].text;     lnamelabel.text = searchgrid.selectedrow.cells[2].text;      string constr = @"provider=microsoft.jet.oledb.4.0; data source=d:\sites\network2\nrsh\app_data\mydb.mdb";     string cmdstr = "select studentid studentlist = searchgrid.selectedrow.cells[3].text , insert journalentries (topic, subtopic, summary, date, notes) values (@topic, @subtopic, @summary, @date, @notes)";      oledbconnection con = new oledbconnection(constr);     oledbcommand com = new oledbcommand(cmdstr, con);     con.open();     //the following fields added journal entry form corresponding database fields     com.parameters.addwithvalue("@topic", ddltopic.text);     com.parameters.addwithvalue("@subtopic", txtsubtopic.text);     com.parameters.addwithvalue("@date", txtdate.text);     com.parameters.addwithvalue("@summary", txtsummary.text);     com.parameters.addwithvalue("@notes", txtnotes.text);     com.executenonquery();     con.close(); } 

so that's thought on logic it's wrong since don't know sql. great.

you cannot combine select , insert statement.

.......

string myvalue = searchgrid.selectedrow.cells[3].text;

you don't have value of searchgrid.selectedrow.cells[3].text in string..you literally have "searchgrid.selectedrow.cells[3].text"...

string myvalue = searchgrid.selectedrow.cells[3].text; string cmdstr = "insert journalentries (studentid , topic, subtopic, summary, date, notes) values (@studentid , @topic, @subtopic, @summary, @date, @notes)"; 

and later

com.parameters.addwithvalue("@studentid", myvalue); com.parameters.addwithvalue("@topic", ddltopic.text); com.parameters.addwithvalue("@subtopic", txtsubtopic.text); com.parameters.addwithvalue("@date", txtdate.text); com.parameters.addwithvalue("@summary", txtsummary.text); com.parameters.addwithvalue("@notes", txtnotes.text); 

i'm kinda guessing. should give table names , columns in table , datatypes (int, string, etc).

edit: refactor.

you should create class accepts arguments. should never have database code .... sitting gui code is.

public class journalentriesmanager {  public static void insertjournalentry ( string studentid, string topic , string subtopic, datetime dateof  , string summary , string notes ) { // code here }  } 

that's "level 1" of "separation of concerns".

but in nutshell, gui level code should collect information controls......and pass info businesslogic ("journalentriesmanager" in case). that.


Comments