|
There is something that escapes me ...
This is the code for the form1
(using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SQLite;
namespace Acode
{
public partial class Form1: Form
{
public Form1 ()
{
InitializeComponent ();
}
private SQLiteConnection sql_con;
private SQLiteCommand sql_cmd;
private SQLiteDataAdapter DB;
private DataSet DS = new DataSet ();
private DataTable DT = new DataTable ();
private void setConnection ()
{
sql_con = new SQLiteConnection (@ "Data Source = DBcode.db; Version = 3; New =; Compress = True;");
}
private void LoadData ()
{
SetConnection ();
sql_con.Open ();
sql_cmd = sql_con.CreateCommand ();
string CommandText = "select * from InfoCode";
DB = new SQLiteDataAdapter (CommandText, sql_con);
DS.Reset ();
DB.Fill (DS);
DT = DS.Tables [0];
dataGridView1.DataSource = DT;
sql_con.Close ();
}
private void Form1_Load (object sender, EventArgs e)
{
LoadData ();
}
private void ExecuteQuery (String txtQuery)
{
SetConnection ();
sql_con.Open ();
sql_cmd = sql_con.CreateCommand ();
sql_cmd.CommandText = txtQuery;
sql_cmd.ExecuteNonQuery ();
sql_con.Close ();
}
public static string randomstring (int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWYZ0123456789";
Random random = new Random ();
return new string (Enumerable.Repeat (floats, length) .Select (s => s [random.Next (s.Length)]). ToArray ());
}
private void btnGenerer_Click (object sender, EventArgs e)
{
lblDisplay.Text = randomstring (4);
txtAfficPrice.Text = "100";
}
private void button1_Click (object sender, EventArgs e)
{
lblDisplay.Text = randomstring (5);
txtAffichPrice.Text = "300";
}
private void button2_Click (object sender, EventArgs e)
{
lblDisplay.Text = randomstring (6);
txtAffichPrice.Text = "500";
}
private void btnRegister_Click (object sender, EventArgs e)
{
if (lblDisplay.Text == "")
{
MessageBox.Show ("Please generate the code");
}
else
{
string txtQuery = "insert into InfoCode (Code, DateCode, PriceCode) values ('" + lblDisplay.Text + "', '" + dateTimePicker1.Text + "', '" + txtAfficePrice.Text + "')";
ExecuteQuery (txtQuery);
LoadData ();
MessageBox.Show ("Registration completed successfully");
lblDisplay.Text = "";
groupBoxGenerer.Enabled = false;
btnSave.Enabled = false;
This.Refresh ();
}
}
private void btnAdd_Click (object sender, EventArgs e)
{
groupBoxGenerer.Enabled = true;
btnSave.Enabled = true;
}
private void exitToolStripMenuItem_Click (object sender, EventArgs e)
{
this.Close ();
}
private void codeSOLDToolStripMenuItem_Click (object sender, EventArgs e)
{
FrmCodeSold frmcodevended = new FrmCodeSold ();
frmcodevendu.Show ();
this.Hide ();
}
private void pointToolStripMenuItem_Click (object sender, EventArgs e)
{
FrmPointJournal pointQ = new FrmPointJournal ();
pointQ.Show ();
this.Hide ();
}
}
}
)
and I wish I could print all the recordings from there via another form
I have a faith done but with access and this faith I use SQlite but as I emphasize them I do not see the data SQlite in my data source.
|
|
|
|
|
For starters, never do this:
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:
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:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable; Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x'; A perfectly valid SELECT
DROP TABLE MyTable; A perfectly valid "delete the table" command
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
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:
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!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you for all these tips for me will be a pulse to my learning. it's in my research that I found but what do you recommend to me?
I correct this error before talking about printing or after?
|
|
|
|
|
I'd correct it first - if only because once you fix the other problem you probably won't "get around to it" otherwise!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Frankly thank you for giving me your time. give me a few minutes just the time for me to redo my connection and I come back to you.
|
|
|
|
|
Not a problem!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SQLite;
namespace Acode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SQLiteConnection sql_con;
private SQLiteCommand sql_cmd;
private SQLiteDataAdapter DB;
private DataSet DS = new DataSet();
private DataTable DT = new DataTable();
private void setConnection()
{
sql_con = new SQLiteConnection(@"Data Source=DBcode.db; Version=3;New=;Compress=True;");
}
private void LoadData()
{
setConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = "select * from InfoCode";
DB = new SQLiteDataAdapter(sql_cmd.CommandText, sql_con);
DS.Reset();
DB.Fill(DS);
DT = DS.Tables[0];
dataGridView1.DataSource = DT;
sql_con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}
private void ExecuteQuery()
{
setConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
sql_con.Close();
}
public static string randomstring(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWYZ0123456789";
Random random = new Random();
return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
}
private void btnGenerer_Click(object sender, EventArgs e)
{
lblAffichage.Text = randomstring(4);
txtAffichPrix.Text = "100";
}
private void button1_Click(object sender, EventArgs e)
{
lblAffichage.Text = randomstring(5);
txtAffichPrix.Text = "300";
}
private void button2_Click(object sender, EventArgs e)
{
lblAffichage.Text = randomstring(6);
txtAffichPrix.Text = "500";
}
private void btnEnregistrer_Click(object sender, EventArgs e)
{
if (lblAffichage.Text =="")
{
MessageBox.Show("Merci de bien vouloir générer le code");
}
else
{
sql_cmd.CommandText = "insert into InfoCode (Code, DateCode, PrixCode) values('" + lblAffichage.Text + "' , '" + dateTimePicker1.Text + "' , '" + txtAffichPrix.Text + "')";
sql_cmd.ExecuteNonQuery();
LoadData();
MessageBox.Show("Enregistrement effectué avec succès");
lblAffichage.Text = "";
groupBoxGenerer.Enabled = false;
btnEnregistrer.Enabled = false;
this.Refresh();
}
}
private void btnAjouter_Click(object sender, EventArgs e)
{
groupBoxGenerer.Enabled = true;
btnEnregistrer.Enabled = true;
}
private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void codeVenduToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmCodeVendu frmcodevendu=new FrmCodeVendu();
frmcodevendu.Show();
this.Hide();
}
private void pointToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmPointJournalier pointQ = new FrmPointJournalier();
pointQ.Show();
this.Hide();
}
}
}
I receive an error message: Database is not open
|
|
|
|
|
No, when I meant was "Don't concatenate strings" and "Parameterized queries" are teh important bit:
sql_cmd.CommandText = "insert into InfoCode (Code, DateCode, PrixCode) values('" + lblAffichage.Text + "' , '" + dateTimePicker1.Text + "' , '" + txtAffichPrix.Text + "')"; Is extremely dangerous, not the "wrong name" bit!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
but what should I do? since you asked me to correct the error. can you tell me more so that I can understand
|
|
|
|
|
Have a google for Parameterized queries - there is a lot out there.
But basically:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
{
cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
cmd.ExecuteNonQuery();
}
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
i will do it aisin and come back to you, thanks for your help
|
|
|
|
|
private void btnAdd_Click(object sender, EventArgs e)
{
if (lblAffichage.Text == "")
{
MessageBox.Show("Merci de bien vouloir générer le code");
}
else
{
using (SQLiteConnection con = new SQLiteConnection(sql_con))
{
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand("INSERT INTO InfoCode (Code, DateCode, PrixCode) VALUES (@Code, @DateCode, @PrixCode)", con))
{
cmd.Parameters.AddWithValue("@Code", lblAffichage.Text);
cmd.Parameters.AddWithValue("@DateCode", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("@PrixCode", txtAffichPrix.Text);
cmd.ExecuteNonQuery();
}
LoadData();
MessageBox.Show("Enregistrement effectué avec succès");
lblAffichage.Text = "";
groupBoxGenerer.Enabled = false;
btnEnregistrer.Enabled = false;
this.Refresh();
}
}
}
Thank you very much for your help ... it works
|
|
|
|
|
Hello sir I was able to find a solution to print, I'm going through a DataSet: I connected the DataSet to my database
|
|
|
|
|
here is the code: {
setConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "select * from InfoCode";
DB = new SQLiteDataAdapter(CommandText, sql_con);
DS.Reset();
CrystalReportImprimCode x = new CrystalReportImprimCode();
DBset dt = new DBset();
DB.Fill(dt.InfoCode);
x.SetDataSource((DataTable)dt.InfoCode);
crystalReportViewer1.ReportSource = x;
crystalReportViewer1.Refresh();
sql_con.Close();
}
|
|
|
|
|
Hello,
I would like to ask you to share information on a subject,
I am experiencing a problem related to data transmission and data retrieval on an Indicator device connected to a serial port on a project I am developing.
Code I Write For Trial Purpose As Below,
SerialPortTLB_DataReceived Event Overflow Should Not Send on Textbox as Byte,
How can I print the incoming data as Bytes?
I'm Waiting For You About This Issue, Good Work ...
using DevComponents.DotNetBar.Metro;
using System;
using System.Windows.Forms;
namespace VeriOkuma
{
public partial class VeriOkuma : MetroForm
{
public VeriOkuma()
{
InitializeComponent();
}
private void BTNVeriGonder_Click(object sender, EventArgs e)
{
if (!SerialPortTLB.IsOpen)
SerialPortTLB.Open();
SerialPortTLB.Write(System.Convert.ToChar(2).ToString() + System.Convert.ToChar(1).ToString() + "DNG" + System.Convert.ToChar(13).ToString());
}
private void VeriOkuma_Load(object sender, EventArgs e)
{
if (SerialPortTLB.IsOpen)
{
SerialPortTLB.Close();
}
}
private void SerialPortTLB_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (!SerialPortTLB.IsOpen) SerialPortTLB.Open();
TXBGelenVeri.Text = SerialPortTLB.ReadExisting();
SerialPortTLB.Close();
}
}
}
modified 28-Mar-18 11:21am.
|
|
|
|
|
It all depends on what format the data is in. You need to check the documentation for the device to see exactly what data is presented when you read from it.
|
|
|
|
|
Hello,
When we contact with the manufacturer company, we inform you as the status on the screen of the device after Char Characters we send.
When the Status Letter arrives, the values are sent correctly.
After this, SerialPort.Read as specified in the company's total length of 41 bytes is required.
SerialPort.ReadExisting () Returns an Incomplete Result.
Features SerialPort.Read Read.
|
|
|
|
|
ibrahimayhans wrote: SerialPort.ReadExisting () Returns an Incomplete Result.
Well, yes. It will.
Serial data is called that because data arrives one bit at a time - a serial stream of data. When seven or eight data bits have been received (together with all appropriate framing characters such as start, stop, and parity bits) the resulkting byte is passed to the input buffer, and the hardware notifies Windows that it is ready. Windows then reads the buffer content, adds it to an internal circular queue, and sends a message to your app which is translated into a DataReceived event.
You handler then gets called and reads the data, however much of it there is.
Subsequent bytes cause the same process to occur. You do not get a "I've got all the data" event, you get an "I've got at least one character for you" event instead. If you want to receive the whole message, then you need to buffer it yourself, and process it until you have a complete "message" built up in your buffer before you start looking at it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I understand State But I am in contact with Producer Company in reply to the query sent in one time 41 byte length is required to be delivered in the value.
What Should I Do to Receive 41 Bytes of Data at a Time?
Also, SerialPort.Read (byte [] buffer, int, int); Can such an operation be done with this method?
|
|
|
|
|
You have to buffer them. You will get upto 41 events - you cannot guarantee exactly how many, it depends on what else is going on in your system.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I did not fully understand the Buffering Scheme or I could not.
Can I send me the edit code on the code I share in the Read operation?
|
|
|
|
|
No - we aren't here to do all your work for you.
If you don't know what you are doing - and it doesn't sound like you have any idea about communications - then you need to start reading and learn, because this is the really easy stuff! If you don't understand what is happening here, nothing else about this job will make any sense, and I will end up doing it all for you.
And trust me, I'd have to charge you for doing your work for you!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
In this case you are right, I've searched too much But I did not do what I did,
I believe that if I can at least understand the logic of this, I will be able to do so without any problem.
On the Yinede You Are So Glad to Help Me in This Concern?
|
|
|
|
|
Stop "searching for the solution" and start reading up on how it all works - this is stuff you need to know in order to work with comms effectively.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
The Producer Company Specially Says 41-Byte Data Future.
|
|
|
|
|