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

C#

 
QuestionHow to print a recording Pin
ago248628-Mar-18 23:31
ago248628-Mar-18 23:31 
AnswerRe: How to print a recording Pin
OriginalGriff28-Mar-18 23:36
mveOriginalGriff28-Mar-18 23:36 
GeneralRe: How to print a recording Pin
ago248628-Mar-18 23:41
ago248628-Mar-18 23:41 
GeneralRe: How to print a recording Pin
OriginalGriff28-Mar-18 23:49
mveOriginalGriff28-Mar-18 23:49 
GeneralRe: How to print a recording Pin
ago248629-Mar-18 0:04
ago248629-Mar-18 0:04 
GeneralRe: How to print a recording Pin
OriginalGriff29-Mar-18 0:11
mveOriginalGriff29-Mar-18 0:11 
GeneralRe: How to print a recording Pin
ago248629-Mar-18 0:20
ago248629-Mar-18 0:20 
GeneralRe: How to print a recording Pin
OriginalGriff29-Mar-18 0:42
mveOriginalGriff29-Mar-18 0:42 
For starters, never do this:
C#
string txtQuery = "insert into InfoCode (Code, DateCode, PriceCode) values ​​('" + lblDisplay.Text + "', '" + dateTimePicker1.Text + "', '" + txtAfficePrice.Text + "')";
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

That won't solve your problem, but it's a major risk that you need to address immediately.
For the problem you have found, start with the debugger.
Put a breakpoint on the line
C#
dataGridView1.DataSource = DT;
and run your app. When it hits the breakpoint, have a close look at DT and see exactly what has been returned. How many columns? How many rows? What is in the actual cells?

As an aside, you need to look at your naming conventions:
C#
private void ExecuteQuery (String txtQuery)
{
        SetConnection ();
sql_con.Open ();
sql_cmd = sql_con.CreateCommand ();
sql_cmd.CommandText = txtQuery;
sql_cmd.ExecuteNonQuery ();
In any code review, a method called ExecuteQuery which calls ExecuteNonQuery would be at target for abusive language! Laugh | :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

GeneralRe: How to print a recording Pin
ago248629-Mar-18 1:05
ago248629-Mar-18 1:05 
GeneralRe: How to print a recording Pin
OriginalGriff29-Mar-18 1:24
mveOriginalGriff29-Mar-18 1:24 
GeneralRe: How to print a recording Pin
ago248629-Mar-18 1:27
ago248629-Mar-18 1:27 
GeneralRe: How to print a recording Pin
OriginalGriff29-Mar-18 1:36
mveOriginalGriff29-Mar-18 1:36 
GeneralRe: How to print a recording Pin
ago248629-Mar-18 2:35
ago248629-Mar-18 2:35 
GeneralRe: How to print a recording Pin
OriginalGriff29-Mar-18 2:46
mveOriginalGriff29-Mar-18 2:46 
GeneralRe: How to print a recording Pin
ago248629-Mar-18 4:04
ago248629-Mar-18 4:04 
GeneralRe: How to print a recording Pin
OriginalGriff29-Mar-18 4:25
mveOriginalGriff29-Mar-18 4:25 
GeneralRe: How to print a recording Pin
ago248629-Mar-18 4:33
ago248629-Mar-18 4:33 
GeneralRe: How to print a recording Pin
ago248629-Mar-18 5:15
ago248629-Mar-18 5:15 
GeneralRe: How to print a recording Pin
ago248611-Apr-18 0:35
ago248611-Apr-18 0:35 
GeneralRe: How to print a recording Pin
ago248611-Apr-18 0:37
ago248611-Apr-18 0:37 
QuestionSerialPort DataReceived Event Read Byte ? Pin
ibrahimayhans28-Mar-18 3:28
ibrahimayhans28-Mar-18 3:28 
AnswerRe: SerialPort DataReceived Event Read Byte ? Pin
Richard MacCutchan28-Mar-18 3:54
mveRichard MacCutchan28-Mar-18 3:54 
GeneralRe: SerialPort DataReceived Event Read Byte ? Pin
ibrahimayhans28-Mar-18 4:00
ibrahimayhans28-Mar-18 4:00 
GeneralRe: SerialPort DataReceived Event Read Byte ? Pin
OriginalGriff28-Mar-18 4:17
mveOriginalGriff28-Mar-18 4:17 
GeneralRe: SerialPort DataReceived Event Read Byte ? Pin
ibrahimayhans28-Mar-18 4:24
ibrahimayhans28-Mar-18 4:24 

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.