✅ how to call a function from another window WPF

I tried using this code but it wasnt working
private void Upload(object sender, RoutedEventArgs e)
{
try
{
int dsCode = Convert.ToInt32(Name_txt.Text);
byte[] images = null;
FileStream stream = new FileStream(imglocation,FileMode.Open,FileAccess.Read);
BinaryReader brs = new BinaryReader(stream);
images = brs.ReadBytes((int)stream.Length);
SqlCommand insertCMD = new SqlCommand("insert into DesignTB(DesignCode,DesignIMG) values(@dsCode,@images)");
insertCMD.Parameters.AddWithValue("@dsCode",dsCode);
insertCMD.Parameters.AddWithValue("@images",images);
int row = dBAccess.executeQuery(insertCMD);
if (row == 1)
{
ManageStock manageStock = new ManageStock();
manageStock.LoadList();
Close();
}
}
catch (Exception)
{
MessageBox.Show("Unsuccessful");
}
}
}
}
private void Upload(object sender, RoutedEventArgs e)
{
try
{
int dsCode = Convert.ToInt32(Name_txt.Text);
byte[] images = null;
FileStream stream = new FileStream(imglocation,FileMode.Open,FileAccess.Read);
BinaryReader brs = new BinaryReader(stream);
images = brs.ReadBytes((int)stream.Length);
SqlCommand insertCMD = new SqlCommand("insert into DesignTB(DesignCode,DesignIMG) values(@dsCode,@images)");
insertCMD.Parameters.AddWithValue("@dsCode",dsCode);
insertCMD.Parameters.AddWithValue("@images",images);
int row = dBAccess.executeQuery(insertCMD);
if (row == 1)
{
ManageStock manageStock = new ManageStock();
manageStock.LoadList();
Close();
}
}
catch (Exception)
{
MessageBox.Show("Unsuccessful");
}
}
}
}
public void LoadList() {
string query = "select * from DesignTB";
dBAccess.readDatathroughAdapter(query, dt);
lstBox.ItemsSource = dt.DefaultView;
dBAccess.closeConn();
}
public void LoadList() {
string query = "select * from DesignTB";
dBAccess.readDatathroughAdapter(query, dt);
lstBox.ItemsSource = dt.DefaultView;
dBAccess.closeConn();
}
45 Replies
SinFluxx
SinFluxx2y ago
What's not working, are you saying the two snippets above are from different files?
Silentgamerz
SilentgamerzOP2y ago
they are from different windows and basically im first uploading data to sql server than using loadlist to refresh the list the load list function is from managestock window
SinFluxx
SinFluxx2y ago
Have you got some shared class that deals with retrieving data from your db?
Silentgamerz
SilentgamerzOP2y ago
yes dBAccess is the class
SinFluxx
SinFluxx2y ago
I'd put your logic from the LoadList() method into that class then, make it return a List instead of void, and then call that from the class for the window that contains the lstBox
Silentgamerz
SilentgamerzOP2y ago
can you show demo code of the function like how would it return list?
SinFluxx
SinFluxx2y ago
Or whatever type dbAccess.readDatathroughAdapter returns?
Silentgamerz
SilentgamerzOP2y ago
it doesnt return anything it just fills the data table
SinFluxx
SinFluxx2y ago
yeah sorry, you'd return dt.DefaultView then
Silentgamerz
SilentgamerzOP2y ago
but i would like to know how to call a function from another window Ill need to use it in the future as well
SinFluxx
SinFluxx2y ago
Well ideally you don't really want to have your windows updating your other windows, they should pretty much be self contained
Silentgamerz
SilentgamerzOP2y ago
private DataTable LoadList()
{
DataTable dt = new DataTable();
string query = "select * from DesignTB";
readDatathroughAdapter(query, dt);
closeConn();
return dt;

}
private DataTable LoadList()
{
DataTable dt = new DataTable();
string query = "select * from DesignTB";
readDatathroughAdapter(query, dt);
closeConn();
return dt;

}
I wrote this code its not really updating other window wait lemme send ss
Silentgamerz
SilentgamerzOP2y ago
Silentgamerz
SilentgamerzOP2y ago
SinFluxx
SinFluxx2y ago
so then you can refresh your list from your window's class with lstBox.ItemsSource = dbAccess.LoadList();
Silentgamerz
SilentgamerzOP2y ago
when i click upload It should kind of send msg which updates or refreshes the list in managestock its not working again back to same thing how do i call function from another window
SinFluxx
SinFluxx2y ago
Is it an error you're getting?
Silentgamerz
SilentgamerzOP2y ago
it is showing build failed
SinFluxx
SinFluxx2y ago
I think you possibly also mean to say different class? What error?
Silentgamerz
SilentgamerzOP2y ago
wpf has windows idk if they are different class partial class it says
SinFluxx
SinFluxx2y ago
Show the actual error
Silentgamerz
SilentgamerzOP2y ago
yea wait
Silentgamerz
SilentgamerzOP2y ago
SinFluxx
SinFluxx2y ago
Try the suggestion then
Silentgamerz
SilentgamerzOP2y ago
i did even after that the build fails
SinFluxx
SinFluxx2y ago
Well the error the build is failing with must be different then, what's that?
Silentgamerz
SilentgamerzOP2y ago
I had forgot to clean the code but even after that it is showing execption
SinFluxx
SinFluxx2y ago
...
Silentgamerz
SilentgamerzOP2y ago
Silentgamerz
SilentgamerzOP2y ago
also u were telling about putting code of load list in dbaccess but i realised it wont work
SinFluxx
SinFluxx2y ago
Why?
Silentgamerz
SilentgamerzOP2y ago
as for that i would need access to lstbox in the class wait let me try and show
SinFluxx
SinFluxx2y ago
That's the point though DbAccess class reads and writes data to your db Your classes for your windows should deal with the presentation DbAccess won't try and set the listbox directly, it will just read the data from the db and pass it back to your other class, which well then deal with how/where it's displayed
Silentgamerz
SilentgamerzOP2y ago
Silentgamerz
SilentgamerzOP2y ago
public void LoadList()
{
ManageStock manageStock = new ManageStock();
DataTable dt = new DataTable();
string query = "select * from DesignTB";
readDatathroughAdapter(query, dt);
manageStock.lstBox.ItemsSource = dt.DefaultView;
closeConn();

}
public void LoadList()
{
ManageStock manageStock = new ManageStock();
DataTable dt = new DataTable();
string query = "select * from DesignTB";
readDatathroughAdapter(query, dt);
manageStock.lstBox.ItemsSource = dt.DefaultView;
closeConn();

}
SinFluxx
SinFluxx2y ago
Ok so what you've done there is, you load up your ManageStock window, which calls dbAccess.LoadList(), which then creates a new ManageStock window, which calls dbAccess.LoadList(), which then creates a new ManageStock window... You see where I'm going?
Silentgamerz
SilentgamerzOP2y ago
oh yea
SinFluxx
SinFluxx2y ago
Your dbAccess class shouldn't be creating a ManageStock window, or trying to set listbox content itself
Silentgamerz
SilentgamerzOP2y ago
but all this would be not needed if my orignal code would work my head is aching ill try to solve this after a while
SinFluxx
SinFluxx2y ago
Well you can go back to your original way of doing it, and having your LoadList method in the same class as the window where your listBox is It's often desirable to have your code separated by concerns, like I mentioned above the class for your window just deals with the layout etc, your dbAccess class just deals with reading/writing data to your db etc
Silentgamerz
SilentgamerzOP2y ago
hey I found this code online but i dont really understand how to implement it in my code
You can assign the Owner to the window that was created in your MainWindow.

window.Owner = this; //This is added to the code that use to create your Window
You can assign the Owner to the window that was created in your MainWindow.

window.Owner = this; //This is added to the code that use to create your Window
Then you should be able to access it something like this.

((MainWindow)this.Owner).Test();
MainWindow
Then you should be able to access it something like this.

((MainWindow)this.Owner).Test();
MainWindow
public partial class MainWindow : Window
{
Window1 window = new Window1();
public MainWindow()
{
InitializeComponent();
window.Show();


}

public void Test()
{
label1.Content += " works";
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
window.Owner = this;
}


}
public partial class MainWindow : Window
{
Window1 window = new Window1();
public MainWindow()
{
InitializeComponent();
window.Show();


}

public void Test()
{
label1.Content += " works";
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
window.Owner = this;
}


}
Second Window
```public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();


}

private void Button_Click(object sender, RoutedEventArgs e)
{
((MainWindow)this.Owner).Test();
}
}
```public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();


}

private void Button_Click(object sender, RoutedEventArgs e)
{
((MainWindow)this.Owner).Test();
}
}
Mayor McCheese
I wouldn't try to call between windows in any meaningful way tbh; consider more like something like a publish subscribe model
Silentgamerz
SilentgamerzOP2y ago
do you have any documents or tutorial link for doing that?
Mayor McCheese
I don't in particular; the simplest mechanism is something like a Channel<T>, which allows you to write data to the channel and have a reader in another location pick up the work I'll try and write a sample; I just can't right at the moment
Silentgamerz
SilentgamerzOP2y ago
Please do It would help me a lot

Did you find this page helpful?