Henry Navarro
Henry Navarro

Reputation: 25

Reference XAML TextBlock to .cs code?

first of all thanks for taking your time, now the problem i have is this; I have different textblocks in my main XAML file, im trying to get their text to use them in a sqlquery from another file(.cs file)

    public void FillTable()
    {
        MainWindow l = new MainWindow();

        string sql = "insert into Pacientes (nombre) values ('"+ l.nombre_text.Text +"')";
        var command = new SQLiteCommand(sql, m_dbConnection);
        command.ExecuteNonQuery();
        l.Close();                

    } 

However, when i check the Table the result is null, when i check the table the "nombre" column is blank

See sql image

any clue what im doing wrong?

Upvotes: 1

Views: 171

Answers (1)

Kory Gill
Kory Gill

Reputation: 7163

You can't do a "new MainWindow". You need to get a reference to your MainWindow or somehow pass in the text to FillTable().

Without seeing more of your code, an exact solution is not probable, but something along these lines might get you unblocked.

...
// in MainWindow.cs
FillTable(this);
...

public void FillTable(MainWindow window)
{
    string sql = "insert into Pacientes (nombre) values ('"+ window.nombre_text.Text +"')";
    var command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();   
} 

OR

...
FillTable(nombre_text.Text);
...

public void FillTable(string nombre)
{
    string sql = "insert into Pacientes (nombre) values ('"+ nombre +"')";
    var command = new SQLiteCommand(sql, m_dbConnection);
    command.ExecuteNonQuery();   
} 

Upvotes: 1

Related Questions