c# - Description not based on database -
i can retrieve data database , works now, on first line (both description , code correct , based on database).
on second line, description not based on database, display description first line, though code first , second line different. how fix that?
here code:
private void updatedatas() { int codevalue = 0; oledbdatareader dreader; oledbconnection conn = new oledbconnection(connectionstring); conn.open(); oledbcommand cmd = new oledbcommand( "select [description], [price] [data] [code]=@code", conn); cmd.parameters.add("code", system.data.oledb.oledbtype.integer); cmd.parameters.add("code", system.data.oledb.oledbtype.integer); if (int.tryparse(this.textboxcodecontainer[0][0].text, out codevalue)) { cmd.parameters["code"].value = codevalue; } else if (int.tryparse(this.textboxcodecontainer[0][1].text, out codevalue)) { cmd.parameters["code"].value = codevalue; } else { messagebox.show("error"); } dreader = cmd.executereader(); while (dreader.read()) { if (textboxcodecontainer[0][0].textlength != 0) { this.textboxdesccontainer[0][0].text = dreader["description"].tostring(); this.textboxsubtotalcontainer[0][0].text = dreader["price"].tostring(); } if (textboxcodecontainer[0][1].textlength != 0) { this.textboxdesccontainer[0][1].text = dreader["description"].tostring(); this.textboxsubtotalcontainer[0][1].text = dreader["price"].tostring(); } } dreader.close(); conn.close(); }
here image:
here image of database:
that's because processes first record twice in loop, both text boxes. try quick fix:
int index = 0; while (dreader.read()) { if (textboxcodecontainer[0][index].textlength != 0) { this.textboxdesccontainer[0][index].text = dreader["description"].tostring(); this.textboxsubtotalcontainer[0][index].text = dreader["price"].tostring(); } index += 1; }
the second problem, add 2 values 1 parameter (code) in query, result of select contain 1 row. should "in" sql keyword. second quick fix concern query:
var query = "select [description], [price] [data] [code] in ("; if (int.tryparse(this.textboxcodecontainer[0][0].text, out codevalue)) { query = query + codevalue.tostring(); } if (int.tryparse(this.textboxcodecontainer[0][1].text, out codevalue)) { query = query + "," + codevalue.tostring(); } query = query + ")"; oledbcommand cmd = new oledbcommand(query, conn); dreader = cmd.executereader();
how parametrize query "in" clause problem - quick fix make work.
Comments
Post a Comment