C
C#2y ago
Duck

❔ How To Use Saved String Data As ButtonLabel Content

Hi I'm fairly new to C#/XAML, however not necessarily new to coding in general (using Visual Studio). I'm building a game launcher of some sort, and I'm trying to display a button content with saved string data, so that its displayed to the user each time the window is displayed. I get my string data from my SQL database
sql = "SELECT CNAME FROM TABLE WHERE MEMBERID = '" + user + "'";
command = new SqlCommand(sql, cnn);
dataReader = command.ExecuteReader();
while (dataReader.Read())
{
Output = (string)dataReader.GetValue(0);
if(char1 == "")
{
char1 = Output;
}
else if (char2 == "")
{
char2 = Output;
}
else if (char3 == "")
{
char3 = Output;
}
}
sql = "SELECT CNAME FROM TABLE WHERE MEMBERID = '" + user + "'";
command = new SqlCommand(sql, cnn);
dataReader = command.ExecuteReader();
while (dataReader.Read())
{
Output = (string)dataReader.GetValue(0);
if(char1 == "")
{
char1 = Output;
}
else if (char2 == "")
{
char2 = Output;
}
else if (char3 == "")
{
char3 = Output;
}
}
And I want to use the data inside char1/char2/char3 as button content However in doing this, I have failed multiple times My code starting off, basically what I expected the end result to be:
namespace UGCAppp
{
/// <summary>
/// Interaction logic for SelectChar.xaml
/// </summary>
public partial class SelectChar : Window
{
public SelectChar()
{
InitializeComponent();
UserIDLoggedIn.Content = "Logged in as: " + UGCLauncher.userId;
this.DataContext = this;
CharSlot1.Content = UGCLauncher.char1;
CharSlot2.Content = UGCLauncher.char2;
CharSlot3.Content = UGCLauncher.char3;
}
}

}
namespace UGCAppp
{
/// <summary>
/// Interaction logic for SelectChar.xaml
/// </summary>
public partial class SelectChar : Window
{
public SelectChar()
{
InitializeComponent();
UserIDLoggedIn.Content = "Logged in as: " + UGCLauncher.userId;
this.DataContext = this;
CharSlot1.Content = UGCLauncher.char1;
CharSlot2.Content = UGCLauncher.char2;
CharSlot3.Content = UGCLauncher.char3;
}
}

}
where UserIDLoggedIn = Label and CharSlot1,2,3 = Buttons, and UGCLauncher.userId, UGCLauncher.char1... = strings with stored values inside I've tried multiple attempts at binding but none seem to generate the end result I need. Here is the XAML code:
<Button x:Name="CharSlot1" Content="{Binding cha1, Mode=OneWay}" HorizontalAlignment="Center" Margin="0,102,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
<Button.Background>
<ImageBrush ImageSource="/button8.png"/>
</Button.Background>
</Button>
<Button x:Name="CharSlot2" Content="" HorizontalAlignment="Center" Margin="0,217,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
<Button.Background>
<ImageBrush ImageSource="/button9.png"/>
</Button.Background>
</Button>
<Button x:Name="CharSlot3" Content="" HorizontalAlignment="Center" Margin="0,330,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
<Button.Background>
<ImageBrush ImageSource="/button10.png"/>
</Button.Background>
</Button>
<Button x:Name="CharSlot1" Content="{Binding cha1, Mode=OneWay}" HorizontalAlignment="Center" Margin="0,102,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
<Button.Background>
<ImageBrush ImageSource="/button8.png"/>
</Button.Background>
</Button>
<Button x:Name="CharSlot2" Content="" HorizontalAlignment="Center" Margin="0,217,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
<Button.Background>
<ImageBrush ImageSource="/button9.png"/>
</Button.Background>
</Button>
<Button x:Name="CharSlot3" Content="" HorizontalAlignment="Center" Margin="0,330,0,0" VerticalAlignment="Top" Height="58" Width="240" Click="Button_Click" FontWeight="Bold" FontSize="20">
<Button.Background>
<ImageBrush ImageSource="/button10.png"/>
</Button.Background>
</Button>
Attached is picture of what happens in the application (char1, char2, and char3 data isnt displayed as content) im fairly new and i feel like its something really obvious im missing ;;
10 Replies
Anton
Anton2y ago
the strings you assign seem to be empty also you're checking for empty strings in your conditions, use string.IsNullOrEmpty (strings are reference types, not just slices like span)
Duck
DuckOP2y ago
once the sql function runs the strings return with values inside of them converted into string, and this function runs before the application loads into this window
Anton
Anton2y ago
I mean when you assign Content
Duck
DuckOP2y ago
sonicThink does every window load at once when u start an application ? if it does i think im having a serious 5head moment
Anton
Anton2y ago
idk it depends how you load it but it's single-threaded most likely unless you do something weird
Duck
DuckOP2y ago
oh i thought it does .... and thought u meant when the application start, then this function also starts
public SelectChar()
{
InitializeComponent();
UserIDLoggedIn.Content = "Logged in as: " + UGCLauncher.userId;
this.DataContext = this;
CharSlot1.Content = UGCLauncher.char1;
CharSlot2.Content = UGCLauncher.char2;
CharSlot3.Content = UGCLauncher.char3;
}
public SelectChar()
{
InitializeComponent();
UserIDLoggedIn.Content = "Logged in as: " + UGCLauncher.userId;
this.DataContext = this;
CharSlot1.Content = UGCLauncher.char1;
CharSlot2.Content = UGCLauncher.char2;
CharSlot3.Content = UGCLauncher.char3;
}
which is why the strngs would be empty
Anton
Anton2y ago
static constructors run lazily when the class is first accessed if that's what you mean
Duck
DuckOP2y ago
I did a small test on my side... turns out they were initially empty in the first place Nanicat
Anton
Anton2y ago
yeah using a debugger for this is usually the way to go
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server