how to refresh the combo box in c#? this the codes - c#

string query = "SELECT * FROM inv.product;";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDatabase = new MySqlCommand(query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDatabase.ExecuteReader();
while (myReader.Read())
{
string sprodID = myReader.GetString("productID");
cmbdel.Items.Add(sprodID);
}
}

Create a function of you existing code like this,
private void BindCombo()
{
cmbdel.Items.Clear(); // Clear the combobox items here
// And bind again...
string query = "SELECT * FROM inv.product;";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDatabase = new MySqlCommand(query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDatabase.ExecuteReader();
while (myReader.Read())
{
string sprodID = myReader.GetString("productID");
cmbdel.Items.Add(sprodID);
}
}
}
Now you can call this method everytime when you delete the item.
Hope it works, thanks.

You can do either of the following to remove an item from the combo box.
cmbdel.Items.RemoveAt(index);
cmbdel.Items.Remove(Value);
Before assigning new set of values,make sure that you call cmbdel.Items.Clear(). It will clear all the items from the combo box.

Related

Filtering listbox with textbox

I know this question might of been asked a couple of times but I looked around and I couldn't find the right thing that would work for me.
So, here's my question.
I currently have a listbox that takes the column "name" out of a mysql database.
As soon as the application starts up it loads all the names in the listbox.
But because there could be alot of names I would like a filter option.
So, if you have for example Mark Jones, Billy Peter in the listbox and you would type Mark in the textbox only Mark Jones would show up.
Fill list box initialize component:
void fill_listbox()
{
string constring = "datasource=localhost;port=3306;username=root;password=xdmemes123";
string Query = "select * from life.players ;";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString("name");
namelistbox.Items.Add(sName);
}
}catch (Exception ex)
{
MessageBox.Show("Something went wrong. Error copied to clipboard.");
Clipboard.SetText(ex.Message);
}
}
The textbox is called "SrchBox" and the listbox "namelistbox"
I've tried a couple of things but it never seemed to work.
Thanks.
Instead of assigning the names from the SQL query directly to the listbox, assign them to a list or an array that you can then filter on later:
List<string> playerNames;
void fill_listbox()
{
string constring = "datasource=localhost;port=3306;username=root;password=xdmemes123";
string Query = "select * from life.players ;";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sName = myReader.GetString("name");
playerNames.Add(sName);
}
foreach(string name in playerNames)
{
namelistbox.Items.Add(name);
}
}catch (Exception ex)
{
MessageBox.Show("Something went wrong. Error copied to clipboard.");
Clipboard.SetText(ex.Message);
}
}
You can then add an event handler for the TextBox's TextChanged event:
void textBox_TextChanged(object sender, EventArgs e)
{
// create filtered list from playerNames
var filteredList = from name in playerNames
where CultureInfo.CurrentCulture.CompareInfo.IndexOf(name, textBox.Text, CompareOptions.IgnoreCase) >= 0
select name;
namelistbox.Items.Clear();
foreach (string name in filteredList)
namelistbox.Items.Add(name);
}

Check radio button in RadioButtonList based on text value read from database

In the user interface, i have radio button list which contains (Level One, Level Two, Level Three). On the other hand, I have student table(ID,name,level,DOB,....) which save the the student's level as varchar.
In page load, i want to fill the radioButtonList based on the value read from database.
the following code is running but it does not check the suitble radio button which read it from DB.
using (MySqlConnection SqlCon = new MySqlConnection(connStr))
{
MySqlDataReader myReader = null;
using (MySqlCommand cmd = new MySqlCommand("SELECT S_Id, level FROM student where S_Id='" + 111 + "'"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = SqlCon;
SqlCon.Open();
myReader = cmd.ExecuteReader();
if (RadioButtonList1.Items.FindByValue(myReader.ToString()) != null)
{
// RadioButtonList1.Items.FindByValue(myReader.ToString()).Selected = true;
RadioButtonList1.SelectedValue = myReader.ToString();
}
SqlCon.Close();
}
}
You have to traverse through the DataReader object to get its values. Try something like this:
using (MySqlConnection SqlCon = new MySqlConnection(connStr))
{
MySqlDataReader myReader = null;
using (MySqlCommand cmd = new MySqlCommand("SELECT S_Id, level FROM student where S_Id='" + 111 + "'"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = SqlCon;
SqlCon.Open();
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
if (RadioButtonList1.Items.FindByValue(myReader["level"].ToString()) != null)
{
RadioButtonList1.SelectedValue = myReader["level"].ToString();
}
}
sqlCon.Close();
}
}

Get Text and int Form Mysql and store in a text box

I am making an application witch uses a Browser to show a viral video a week.The url for the video is stored in a my MySQL database online along with an int that shows a counter in the top right hand corner, What I want to do is get the string of text from the MySQL data base and the in and write that information into a text box called Video1 and Counter.
Here is some of my current code:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = Convert.ToInt32(Refresh_Rate.Text);
timer1.Enabled = true;
try
{
string myConnrction = "datasource=************;port=3306;username=*******;password=*******";
MySqlConnection myConn = new MySqlConnection(myConnrction);
MySqlCommand SelectCommand = new MySqlCommand("select * from sql337069.Youtube where Video1='", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
while (myReader.Read())
{
}
MySqlDataAdapter myDataAdapter =new MySqlDataAdapter();
myDataAdapter.SelectCommand = new MySqlCommand(" select * sql337069.Youtube ;", myConn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
myConn.Open();
DataSet ds = new DataSet();
MessageBox.Show("Working");
myConn.Close();
}
catch
{
MessageBox.Show("Null");
}
So I guess I am looking for is something like Video1.text += [?].
From what I can see, it would be better for you to grab the columns that you want in your select statement, rather than selecting all column (*). When you select the ones you want, you know the exact structure of the data that is being returned and you can hold that in a class or structure that you create. say, something like this for example:
public struct Video
{
public int ID;
public string Name;
public int Count;
}
static void Main(string[] args)
{
string myConnectionString = "YOUR_CONNECTION_STRING";
SqlConnection myConn = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand("SELECT ID, VideoName, VideoPlayCount FROM dbo.VideoTable WHERE Id = YOUR_VIDEO_RECORD_ID", myConn);
Video video = new Video();
myCommand.CommandType = System.Data.CommandType.Text;
myConn.Open();
SqlDataReader reader = myCommand.ExecuteReader(CommandBehavior.SingleResult);
reader.Read();
video.ID = reader.GetInt32(0);
video.Name = reader.GetString(1);
video.Count = reader.GetInt32(2);
myConn.Close();
string myTextBoxValue = video.Name + " " + video.Count.ToString();
}
Using this example would require you to replace the "YOUR_CONNECTION_STRING" with the connection string to your database, as well as entering in the correct table name and table column for the select statement, but other than that, this should get you what you are looking for I believe. The string "myTextBoxValue" is being assigned the video name and count as requested.
Hope this helps.
For Me i Have discovered that this method works best But thanks to Kevin for helping me fill in some code.
try
{
//Defanitions
string myConnrction = "*********;port=3306;username=******;password=******";
MySqlConnection myConn = new MySqlConnection(myConnrction);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.SelectCommand = new MySqlCommand(" select * sql337069.Youtube;", myConn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
DataSet ds = new DataSet();
string Query = "select * from sql337069.Youtube ;";
MySqlCommand cmdDataBase = new MySqlCommand(Query, myConn);
MySqlDataReader myreader;
//Defanitions
myConn.Open();
myreader = cmdDataBase.ExecuteReader();
while (myreader.Read())
{
string sName = myreader.GetString("Video2_Refresh");
Video2_Refresh.Text = sName;
}
MessageBox.Show("Working");
}
catch
{
MessageBox.Show("Null");
}
(I am posting this for stack overflow reference Because i could not find much on this subject )

C# dataReader not working properly

I am trying to take a single value from a database, but the cycle does not seem to start at all?
if (connection.State == ConnectionState.Open)
{
MySqlDataReader dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
value = dataReader["amount"].ToString();
}
dataReader.Close();
connection.Close();
}
These are the commands:
public string value;
public static string Konekcija = "Server=127.0.0.1; Database=CardIgrica; Uid=admin; Pwd=admin;";
public string komanda = "SELECT amount FROM CardIgrica.creaures WHERE id = '1';";
MySqlConnection connection = new MySqlConnection(Konekcija);
MySqlCommand cmd = new MySqlCommand(komanda, connection);
connection.Open();
try this for the select
public string value {get; set;}
string komanda ="SELECT amount FROM CardIgrica.creaures WHERE id = 1";
Try this
using (SqlConnection conn = new SqlConnection(Konekcija))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
cmd.CommandText = komanda ;
conn.Open();
var dataReader= cmd.ExecuteReader();
while (result.Read())
{
value = dataReader["amount"].ToString();
}
conn.Close();
}
If you need single value, you can use cmd.ExecuteScalar(); instead of cmd.ExecuteReader();
because ExecuteScalar() will return single value.
First open your sql connection then make object of MySqlCommand like below:
MySqlConnection connection = new MySqlConnection(Konekcija);
connection.Open();
MySqlCommand cmd = new MySqlCommand(komanda, connection);

How to display images from mysqldatabase using c# in WPF?

string constring = "server=localhost;uid=root;" + "pwd=12345;database=products;";
string query = "SELECT prd_items_image FROM products.prd_items where prd_items_id=5";
MySqlConnection condatabase = new MySqlConnection(constring);
MySqlCommand cmddatabase = new MySqlCommand(query, condatabase);
MySqlDataReader myreader;
try
{
condatabase.Open();
myreader = cmddatabase.ExecuteReader();
while (myreader.Read())
{
byte[] imgg = (byte[])(myreader["prd_items_image"]);
if (imgg == null)
box.Image = null;
else
{
MemoryStream mstream = new MemoryStream(imgg);
box.Image = System.Drawing.Image.FromStream(mstream);
}
}
I am getting error at if condition,
box.Image=null andbox.Image = System.Drawing.Image.FromStream(mstream);
In these two cases, I am getting error at Image.
So please check it once. If you have any other code ,provide me if required.

Categories

Resources