C
C#16mo ago
Silentgamerz

✅ 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
SinFluxx16mo ago
What's not working, are you saying the two snippets above are from different files?
Silentgamerz
SilentgamerzOP16mo 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
SinFluxx16mo ago
Have you got some shared class that deals with retrieving data from your db?
Silentgamerz
SilentgamerzOP16mo ago
yes dBAccess is the class
SinFluxx
SinFluxx16mo 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
SilentgamerzOP16mo ago
can you show demo code of the function like how would it return list?
SinFluxx
SinFluxx16mo ago
Or whatever type dbAccess.readDatathroughAdapter returns?
Silentgamerz
SilentgamerzOP16mo ago
it doesnt return anything it just fills the data table
SinFluxx
SinFluxx16mo ago
yeah sorry, you'd return dt.DefaultView then
Silentgamerz
SilentgamerzOP16mo 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
SinFluxx16mo ago
Well ideally you don't really want to have your windows updating your other windows, they should pretty much be self contained
Silentgamerz
SilentgamerzOP16mo 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
SilentgamerzOP16mo ago
Silentgamerz
SilentgamerzOP16mo ago
SinFluxx
SinFluxx16mo ago
so then you can refresh your list from your window's class with lstBox.ItemsSource = dbAccess.LoadList();
Silentgamerz
SilentgamerzOP16mo 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
SinFluxx16mo ago
Is it an error you're getting?
Silentgamerz
SilentgamerzOP16mo ago
it is showing build failed
SinFluxx
SinFluxx16mo ago
I think you possibly also mean to say different class? What error?
Silentgamerz
SilentgamerzOP16mo ago
wpf has windows idk if they are different class partial class it says
SinFluxx
SinFluxx16mo ago
Show the actual error
Silentgamerz
SilentgamerzOP16mo ago
yea wait
Silentgamerz
SilentgamerzOP16mo ago
SinFluxx
SinFluxx16mo ago
Try the suggestion then
Silentgamerz
SilentgamerzOP16mo ago
i did even after that the build fails
SinFluxx
SinFluxx16mo ago
Well the error the build is failing with must be different then, what's that?
Silentgamerz
SilentgamerzOP16mo ago
I had forgot to clean the code but even after that it is showing execption
SinFluxx
SinFluxx16mo ago
...
Silentgamerz
SilentgamerzOP16mo ago
Silentgamerz
SilentgamerzOP16mo ago
also u were telling about putting code of load list in dbaccess but i realised it wont work
SinFluxx
SinFluxx16mo ago
Why?
Silentgamerz
SilentgamerzOP16mo ago
as for that i would need access to lstbox in the class wait let me try and show
SinFluxx
SinFluxx16mo 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
SilentgamerzOP16mo ago
Silentgamerz
SilentgamerzOP16mo 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
SinFluxx16mo 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
SilentgamerzOP16mo ago
oh yea
SinFluxx
SinFluxx16mo ago
Your dbAccess class shouldn't be creating a ManageStock window, or trying to set listbox content itself
Silentgamerz
SilentgamerzOP16mo 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
SinFluxx16mo 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
SilentgamerzOP16mo 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
Mayor McCheese16mo ago
I wouldn't try to call between windows in any meaningful way tbh; consider more like something like a publish subscribe model
Silentgamerz
SilentgamerzOP16mo ago
do you have any documents or tutorial link for doing that?
Mayor McCheese
Mayor McCheese16mo ago
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
SilentgamerzOP16mo ago
Please do It would help me a lot
Want results from more Discord servers?
Add your server