I have a group box contains radio buttons eg.
o Level 1
o Level 2
How can I load value from database and check radio botton on my GUI?
private void button_clone_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT * from PPAPdatabase where [PSW ID]=" + txt_c_PSW_ID.Text + "";
OleDbDataReader dr = null;
dr = command.ExecuteReader();
while (dr.Read())
{
comboBox_PPAP.Text = (dr["Reason"].ToString());
checkedListBox_prodline.Text = (dr["Production Line"].ToString());
checkedListBox_owner.Text = (dr["Owner"].ToString());
txt_comment.Text = (dr["Comment"].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show("An error has occurred: " + ex.Message,
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
finally
{
connection.Close();
}
Thanks in advance!
Assuming your radio button is called radioButton1 and the column your referring to stores values as either 1 or 0 then you would do:
radioButton1.Checked = row["PPAP"].ToString() == "1";
Related
Form 1 and 2
how do i auto select after i click search from another form ?
iam using C#. the textbox below only for store(check) i want the form 1 with gridview is selected
my code below on form 2 :
private void simpleButton1_Click(object sender, EventArgs e)
{
cari();
}
public void cari()
{
SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, myConnection);
try
{
ds.Tables.Clear();
command.Connection = myConnection;
command.CommandText = "select * from General.genre WHERE genre Like '%"+textEdit1.Text.ToString()+"%' or code like '%"+textEdit1.Text+"%'";
myConnection.Open();
var buka = command.ExecuteReader();
if (buka.Read())
{
textEdit2.Text = buka[1].ToString();
}
else
{
MessageBox.Show("type genre");
}
// MessageBox.Show("kebaca");
}
catch (System.Exception ex)
{
MessageBox.Show("error" + ex);
}
myConnection.Close();
}
dataGrid.SelectedRows.Clear();
foreach(DataGridViewRow row in dataGrid.Rows)
{
if(YOUR CONDITION)
row.Selected = true;
}
I would like to get the value from ms access database to the checkedListBox.
it works properly for the ComboBox and TextBox but I don't know how to do that with the checkedListBox_prodline or checkedListBox_owner. (I have only one value in the database field)
private void button_clone_Click(object sender, EventArgs e)
{
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "SELECT * from PPAPdatabase where [PSW ID]=" + txt_c_PSW_ID.Text + "";
OleDbDataReader dr = null;
dr = command.ExecuteReader();
while (dr.Read())
{
comboBox_PPAP.Text = (dr["Reason"].ToString());
checkedListBox_prodline.Text = (dr["Production Line"].ToString());
checkedListBox_owner.Text = (dr["Owner"].ToString());
txt_comment.Text = (dr["Comment"].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show("An error has occurred: " + ex.Message,
"Important Note",
MessageBoxButtons.OK,
MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1);
}
finally
{
connection.Close();
}
Any help would be greatly appreciated!
Take a look of CheckedListBox.SetItemChecked. In case your items are strings.
var productLine = dr["Production Line"].ToString();
for (var i = 0; i < checkedListBox_prodline.Items.Count; i++)
{
var item = checkedListBox_prodline.Items[i] as string;
checkedListBox_prodline.SetItemChecked(i, productLine == item);
}
so I just want to view the values from my database to some textboxes in my project whenever the selected index of listbox changes
I have these code but whenever I click an item on my listbox, none of the textboxes displays a data or anything
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringHRMS"].ConnectionString);
conn.Open();
string selectone = "SELECT * FROM Applicant WHERE ApplicationNo = '" + ListBox1.SelectedValue.ToString() + "'";
SqlCommand cmd = new SqlCommand(selectone, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
asln.Text = reader["LastName"].ToString();
asfn.Text = reader["FirstName"].ToString();
asmn.Text = reader["MiddleName"].ToString();
ashea.Text = reader["HEA"].ToString();
astitle.Text = reader["Title"].ToString();
aspos.Text = reader["Position"].ToString();
asaddress.Text = reader["Address"].ToString();
asbday.Text = reader["Birthday"].ToString();
asgender.Text = reader["Gender"].ToString();
asage.Text = reader["Age"].ToString();
asht.Text = reader["Height"].ToString();
aswt.Text = reader["Weight"].ToString();
asemail.Text = reader["Email"].ToString();
ascontact.Text = reader["ContactNumber"].ToString();
}
reader.Close();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
I want to show all details of the member through the name selected in combo box..Im trying below given code
private void cbSearchByName_SelectedIndexChanged(object sender,EventArgs e)
{
try
{
//int RowsAffected = 0;
DataAccess oDataAccess = new DataAccess();
con.Open();
//showing flat number of selected member by name
oDataAccess.cmd.CommandText = "SELECT FlatNo FROM MemberInfo where MemberName='" + cbSearchByName.Text + "'";
oDataAccess.cmd.Connection = con;
tbOwnerName.Text = ((string)cmd.ExecuteScalar());
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
I understand that you need to get all column records from memberinfo table and display them in UI. For that, you need to use ExecuteReader instead of ExecuteScalar. I have implemented execute reader in below code
private void cbSearchByName_SelectedIndexChanged(object sender,EventArgs e)
{
try
{
//int RowsAffected = 0;
DataAccess oDataAccess = new DataAccess();
using(SqlConnection connection = con )
{
connection.Open();
//showing flat number of selected member by name
oDataAccess.cmd.CommandText = "SELECT Top 1 Name,City FROM MemberInfo where MemberName='" + cbSearchByName.Text + "'";
oDataAccess.cmd.Connection = con;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
tbOwnerName.Text = dr["Name"].ToString();
tbOwnerCity.Text = dr["City"].ToString();
//similarly store other column values in respective text boxes or wherever you need to get it displayed.
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
i have a buttonfield in a gridview, when i press that button it takes the id value from the first column, i use that id in a select statement, to get data from the table, but i get this error "No Value Give For One Or More Parameters"
protected void grdData_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = grdData.SelectedRow;
string id = row.Cells[1].Text;
try
{
conn.ConnectionString = conn_string;
conn.Open();
mySQLCommand.Connection = conn;
mySQLCommand.CommandText = "Select [Movie_Description],[Movie_Image] from Movie_tbl where Movie_ID = #Movie_ID";
mySQLCommand.Parameters.Add("#Movie_ID", OleDbType.VarChar).Value = id;
myDataReader = mySQLCommand.ExecuteReader();
if (myDataReader.Read())
{
txtDescription.Text = myDataReader["Movie_Description"].ToString();
}
else
{
txtDescription.Text = "No Such Movie";
}
}
catch (Exception ex)
{
throw ex ;
}
}
I haven't worked with mySQL much, but I'm pretty sure you don't want an OleDbType.VarChar parameter. I haven't seen that used outside of MS Access. Try:
mySQLCommand.Parameters.AddWithValue("#Movie_ID", id);
if that fails, maybe try
mySQLCommand.Parameters.Add(new MySqlParameter("#Movie_ID", id));
mySQLCommand is of type MySqlCommand in your code, right?