Content Dialog does not display after database insert query - UWP App - c#

I cannot get a UWP content dialog to display after executing a database insert query. Below is the code I have written. The Content dialog displays as it should when the insert query code is commented out. Am I missing something? Any help will be appreciated. Thanks
private async void Admin_Button_Click(object sender, RoutedEventArgs e)
{
String selItem = TaskCombo.SelectedValue.ToString();
dbDetail.AddAdminData(selItem, SettingTxt.Text);
Windows.UI.Xaml.Controls.ContentDialog infoDialog = new Windows.UI.Xaml.Controls.ContentDialog()
{
Title = "Alert",
Content = "Records entered successfully",
CloseButtonText = "OK"
};
Windows.UI.Xaml.Controls.ContentDialogResult result = await infoDialog.ShowAsync();
}

Try to define String selItem as property like this String selItem{set;get;}=TaskCombo.SelectedValue.ToString();

The problem was an open data reader associated with the connection that was open and needed to be closed first. The db query was executing successfully, however the content dialogue could not be display because of the open data reader.

Related

RichTextBox.RTF not accepting String generated from Database - C#

I have a bit of a weird problem here. I'm attempting to create a form that, when making a selection from a ListBox, will poll data from a database and display it in a RichTextBox. I need the data to be in RTF for formatting purposes.
It works fine if I do something like this:
private void SaveListTest_SelectedIndexChanged(object sender, EventArgs e)
{
DescriptionName = Convert.ToString(SaveListTest.SelectedItem);
//CallDescriptionTest();
CallDescriptionTest2();
SaveRichTest.Rtf = DescriptionText;
}
public void CallDescriptionTest2()
{
switch (DescriptionName)
{
case "Test":
DescriptionText = #"{\rtf1\ansi\ Test}";
break;
case "Words":
DescriptionText = #"{\rtf1\ansi\ A really long phrase}";
break;
}
}
In such a case, the RichTextBox (SaveRichTest) will take the data and display it just fine.
However, if I do something like this, where the Description column in the database has the text entered exactly as above (ex - #"{\rtf1\ansi\ Test}"):
private void SaveListTest_SelectedIndexChanged(object sender, EventArgs e)
{
DescriptionName = Convert.ToString(SaveListTest.SelectedItem);
CallDescriptionTest();
//CallDescriptionTest2();
SaveRichTest.Rtf = DescriptionText;
}
public void CallDescriptionTest()
{
using (SqlConnection con = new SqlConnection(BuildDB))
{
con.Open();
string sql = String.Format("Select * from Abilities where Name = '{0}'", DescriptionName);
SqlCommand oCmd = new SqlCommand(sql, con);
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
DescriptionText = Convert.ToString(oReader["Description"]);
}
con.Close();
}
}
}
This will instead cause the program to crash, with an error that the "File format is not valid".
I know the text is pulling from the database correctly, because if I change "SaveRichTest.Rtf" to "SaveRichTest.Text", it displays it properly (albeit with the RTF formatting code displayed).
I just can't figure out why it won't take the string properly in the second case. It makes no sense to me whatsoever. Can someone help?
I guess in the case when you tried to show your output in plain text your output was: #"{\rtf1\ansi\ Test}
This would mean when you want to show this string formatted directly from the database you would pass it "#"{\rtf1\ansi\ Test}"" instead of the correct format. I'd suggest you store the format without the # in the database and simply show the result.

Passing Variable from one WPF Window to Another (C#)

I really feel bad for having to ask this. The answer should probably be obvious; however, I have searched here and on Google to no avail.
I am quite new to WPF. With Windows Forms, I recall it was quite easy to pass data between Windows, but I cannot figure it out with my WPF application.
I am simply building an application for myself that will allow me to access and insert data in a MySQL database. When the button I have added to view all the records in a table is clicked, I want a new window to open. This window will simply display all the data; the table will be selected in MainWindow.
The problem I have is this: I need someway for the secondary window (View) to know which table was selected. I will probably use a ListBox control for table selection, but at the end of the day it really does not matter. Eventually, the value will be stored as a string in a variable. I need the View window to be able to access this string so it knows which table to query. In example:
// Main Window
private void viewButton_Click(object sender, RoutedEventArgs e)
{
var table = "DS";
View viewWindow = new View();
viewWindow.Show();
}
// View Window
private void windowLoad(object sender, RoutedEventArgs e)
{
DatabaseConnection myConnection = new DatabaseConnection();
List<string>[] result = myConnection.Select(table);
text.Text = MainWindow.test + (result[0][0] + " " + result[1][0] + " " + result[2][0] + " " + result[3][0]);
}
All I need to do is to pass the value of table (which will vary in the final application based on user input), to View. For example, I need it so that in the above code, the following would be executed:
List<string>[] result = myConnection.Select("DS");
I feel like this should be obvious and easy, but I am not sure how to accomplish this. I would sincerely appreciate any help.
You can pass a variable through constructor, like this
public View(string table)
{
this.table = table;
}
And when you initialize your window:
var table = "DS";
View viewWindow = new View(table);
viewWindow.Show();

asyncFileUpload how to get new File Name

asyncFileUpload how to get a new File Name after upload complete to save new name in database
asp.net c#
I have a form to fill in personal data and Upload own image
Asyncfileupload used to select the picture and in the void AsyncFileUpload_UploadedComplete
Save the file in new name
Now when the user ends of the mobilization of all form and presses the save button to save data , i save every thing in database I can not get the new name to save it.
it is a property in the control. In you event handler check the FileName property. The following code assumes your are checking the file size to to intercept any oversized files.
protected void AsyncFileUpload_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
{
AsyncFileUpload upload = sender as AsyncFileUpload;
if ((upload.PostedFile.ContentLength / 1000) <= 500)
{
string filename = upload.FileName;
// save to the database here
}
}
Hope this helps :-)
Mike

Select one string line in a list

I'm trying to simply select one string line out a long list strings that are held on a server and seperated with a pipe character. This string is grabbed by a php script and the string line is a list of all the media and folders I have on my server.
In my code I'm getting this information and returning it with the following code:
using (var client = new WebClient())
{
result = client.DownloadString("http://server.foo.com/images/getDirectoryList.php");
}
textBox1.Text = string.Join(Environment.NewLine, result.Split('|'));
And it looks like this:
But when I try to simply click on one of them, my cursor simply just goes to where I've clicked. Like this, I tried to select md-harrier.jpg and my cursor just ends up at the end of jpg:
What I'm really wanting is pictured below. I click on Koala.jpg and the whole thing is highlighted and I have the ability to store the name of what it is I've just clicked on. TO achieve that screen shot I had to click next to Koala.jpg and then drag my mouse along.
Is there anyway I can achieve what I want to achieve?
The key thing to note about this is that I will have no idea how many files will be on the server, nor what they will be called. My php script is grabbing this information and displaying it in my winform text box using the code I have wrote above.
as Simon said you need a ListBox, a ListBox fits here because it allows you to select a line, and you can register to the event of SelectedIndexChanged and store the name that was selected.
to initiate the values do
using (var client = new WebClient())
{
result = client.DownloadString("http://bender.holovis.com/images/getDirectoryList.php");
}
listBox1.Items.AddRange(result.Split('|'));
listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
and on the selectedItemChanged:
string currVal;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
currVal = (string)listBox1.SelectedItem;
}
As you said you have no reason to use TextBox,then by using ListBox you can achieve that in this way;
using (var client = new WebClient())
{
result = client.DownloadString("http://bender.holovis.com/images/getDirectoryList.php");
}
string[] names=result.Split('|');
foreach(string name in names)
{
if(name!="|"&&name!=" ")
{
listbox.Items.Add(name);
}
}
Additionally,if you would like to store selected item in a variable subscribe to ListBox's SelectionChangedEvent and store the selection index in a variable in this way;
int selection=;
private void ListBox1_SelectionIndexChanged(object sender,EventArgs e)
{
selection=ListBox1.SelectedIndex;
}

C# how to show available data one by one in textbox

Hi I am using HtmlAgilityPack to scrap some data from web using c# . Here is the code :
private void button1_Click(object sender, EventArgs e)
{
var url = this.textBox1.Text;
var webGet = new HtmlWeb();
var document = webGet.Load(url);
var metaTags = document.DocumentNode.SelectNodes("//meta");
if (metaTags != null)
{
foreach (var tag in metaTags)
{
var name = tag.Attributes["name"].Value;
var content = tag.Attributes["content"].Value;
this.textBox2.Text = name + " : " + content;
}
}
}
its getting a link from textbox1 and showing the output to textbox2 . Its showing the last available data . I can concate the available data but it will show all data at once. Actually I want to show one data when it is available while others are being processed so that user can realize the scrapping progress.Would anyone please help ??
Assuming the first half of your code is correct, this will show the data one-by-one. Most likely, the foreach loop is executing too quickly for you to notice the "one-by-one" effect.
You could apply an artificial delay if you need to display each result long enough for someone to read. Maybe with a Timer or something similar.
An alternative is to use a multi-line textbox (for example a RichTextBox) and place each result on a new-line. Or a ComboBox, which won't take up as much room. These options allow you to keep the data, without having to overwrite it.
You can user Timer control to delay the display of next value in the loop.
You can also use BackgroundWorker to get this done. However this would add complexity to your code. Please look at Beginners Guide to Threading in .NET: Part 5
Or you can use Thread.Sleep(xx) which will delay the execution for the milliseconds you specify.
Thread.Sleep(2000);
this.textBox2.Text += name + " : " + content + Environment.NewLine;

Categories

Resources