0

So I have this code I'm using for my project. It should generate the numbers that is inputted in the textbox, but the thing is after I saved the png, once you edit the text in textbox, the QrCode will generate new one. So, for me to avoid to this, I put the textbox on readonly and the data that should be displayed here is the data from database. But it's always empty. This is my code on the form where I will get the data from database.

 private void qrcode_Click(object sender, RoutedEventArgs e)
    {
        QRCode qr = new QRCode();
        qr.Show();
        this.Hide();
        qnum = driver("Select LicenseNumber from driver where FranchiseNumber = '" + fn + "'").ToString();
    }

and this one is where I will pass the data.

public QRCode()
    {
        InitializeComponent();
        textBox1.Text = AddDriver.qnum;
        //textBox1.Text = "hello";
    }

I tried to put hello to see if it will display, and yes it did. But when I'm trying the one above it, there's nothing. Can anybody help me, and tell what's wrong. And also how can I make this work?

user8540439
  • 47
  • 1
  • 9
  • Btw, the '"+ fn +"' I used was declared on the code. And also, Im using WPF on the first code, and WindowsForm on the second one. – user8540439 Sep 21 '17 at 10:59
  • Well you need show how are you passing that string to the other window. If you are passing it as property the property setter is run after the constructor. so you need to set it at a later time. – Filip Cordas Sep 21 '17 at 11:22
  • looks like AddDriver.qnum retuns nothing. Hard to tell offcourse without any code – GuidoG Sep 21 '17 at 15:18
  • yeah, It does return nothing :( I don't know what else to change. Btw, I put the codes below. – user8540439 Sep 21 '17 at 15:47

2 Answers2

1

I have solve this problem, and this is the code I used. Hope it could help.

public partial class AddDriver : Window
{

    MySqlConnection connection = new MySqlConnection();
    MySqlDataAdapter adapter = new MySqlDataAdapter();
    DatabaseController db = new DatabaseController();

    public static string qnum;
    public AddDriver()
    {
        InitializeComponent();
    }
    private void btn_home_Click(object sender, RoutedEventArgs e)
    {
        MainWindow main = new MainWindow();
        main.Show();
        this.Hide();
    }
    public DataTable driver(string query)
    {
        DataTable dtable = new DataTable();
        try
        {
            OpenDbase(query);
            adapter.Fill(dtable);
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return dtable;
    }
    private void OpenDbase(string query)
    {
        try
        {
            string connectionstring = "server=sql12.freemysqlhosting.net; port=3306; user id=sql12192362; password= DtxpressProject25!; persistsecurityinfo=False;database=sql12192362";
            connection = new MySqlConnection(connectionstring);
            adapter.SelectCommand = new MySqlCommand(query, connection);
            MySqlCommandBuilder cb = new MySqlCommandBuilder(adapter);
            connection.Open();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void qrcode_Click(object sender, RoutedEventArgs e)
    {
        string fn = fnum.Text;
        int count = driver("Select * from driver where FranchiseNumber ='" + fn + "'").DefaultView.Count;
        if (count > 0)
        {
            MessageBox.Show("'"+fn+"' is already on the list");
        }
        else
        {
            OpenDbase("Insert into driver (LastName, FirstName,MidInitial,Address,Contact,FranchiseNumber,LicenseNumber,Income, Password) values ('" + this.lname.Text + "','" + this.fname.Text + "','" + this.midint.Text + "','" + this.addrss.Text + "','" + this.contact.Text + "','" + this.fnum.Text + "','" + this.lnum.Text + "', '0', '" + this.fnum.Text + "')");
            DataTable dtable = new DataTable();
            adapter.Fill(dtable);
            connection.Close();
        }

        QRCode qr = new QRCode();
        qr.Show();
        this.Hide();
        qnum = fnum.Text;
    }

And this is where I will pass it.

public partial class QRCode : Form
{
    [SerializableAttribute]
    [ComVisibleAttribute(true)]
    public class ArgumentNullException : ArgumentException { }

    MySqlConnection connection = new MySqlConnection("server=sql12.freemysqlhosting.net; port=3306; user id=sql12192362; password= DtxpressProject25!; persistsecurityinfo=False;database=sql12192362");
    MySqlCommand command;
    public QRCode()
    {
        InitializeComponent();
    }

    private void btn_gen_Click(object sender, EventArgs e)
    {
        Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr;
        pictureBox1.Image = qrcode.Draw(textBox1.Text, 50);

        SaveFileDialog f = new SaveFileDialog();
        f.Filter = "PNG(*.PNG)|*.png";

        if (f.ShowDialog() == DialogResult.OK)
        {

            pictureBox1.Image.Save(f.FileName);
            MessageBox.Show("QR Code has been successfully saved.");
        }
    }

    private void btn_sve_Click(object sender, EventArgs e)
    {
        MemoryStream ms = new MemoryStream();
        byte[] img = ms.ToArray();
        string fn = AddDriver.qnum.ToString();

        String insertQuery = "Update driver set QrCode = '" + @img + "' where FranchiseNumber= '"+fn+"'";

        connection.Open();
        command = new MySqlCommand(insertQuery, connection);
        command.Parameters.Add("@img", MySqlDbType.Blob);

        command.Parameters["@img"].Value = img;

        if (command.ExecuteNonQuery() == 1)
        {
            MessageBox.Show("Driver has been added.");
        }
        connection.Close();
        AddDriver back = new AddDriver();
        back.Show();
        this.Hide();
    }

It's working fine, so far. Also thank you to those who answered.

user8540439
  • 47
  • 1
  • 9
0

Try to set the "qnum" string to a string that contains the values in the DataTable and set it before you show the window:

private void qrcode_Click(object sender, RoutedEventArgs e)
{
    vr dt = driver("Select LicenseNumber from driver where FranchiseNumber = '" + fn + "'");
    qnum = "";
    if(dt != null)
    { 
        foreach(var row in dt.Rows)
            qnum += row["LicenseNumber"].ToString() + ", ";  
    }

    QRCode qr = new QRCode();
    qr.Show();
    this.Hide();  
}

Also note that you should use parameters when executing dynamic SQL queries:

How does SQLParameter prevent SQL Injection?

mm8
  • 163,881
  • 10
  • 57
  • 88