From the title above, Im making a windows form in c# where in 2 of my checkedlistbox should populate the values from 2 tables in the database. However, when I tried to run the code, I always get this exception: "Cannot bind to the new value member".
below is my code. if you could find any errors/suggestions, feel free to comment. It would be a big help. thanks!
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
NpgsqlConnection conn = new NpgsqlConnection(connstring);
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM data_condition", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
dt.TableName = "data_condition";
da.Fill(dt);
checkedListBox1.DataSource = dt;
checkedListBox1.DisplayMember = "conname";
checkedListBox1.ValueMember = "conid";
string[] condition = dt.Rows[0]["conname"].ToString().Split(',');
NpgsqlCommand cmd2 = new NpgsqlCommand("SELECT * FROM data_allergy", conn);
cmd2.CommandType = CommandType.Text;
NpgsqlDataAdapter da2 = new NpgsqlDataAdapter(cmd2);
dt2.TableName = "data_allergy";
da2.Fill(dt2);
checkedListBox2.DataSource = dt2;
checkedListBox2.DisplayMember = "allergyname";
checkedListBox2.ValueMember = "allerygid";
string[] allergy = dt2.Rows[0]["allergyname"].ToString().Split(',');
Related
Hi i have combobox with Table names. I would like to display selected table on button click. How to do it? This is code of my combobox
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT name FROM sys.tables", Con);
SqlDataReader rdr;
rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Load(rdr);
wczytywanie.ValueMember = "name";
wczytywanie.DataSource = dt;
Con.Close();
now i would like to display these tables in dataGridView after button click.This is not working.
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from sys.tables where name=#name", Con);
cmd.Parameters.Add("name", wczytywanie.SelectedValue.ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Dane.DataSource = dt;
Con.Close();
try adding DisplayMember and assign value "name"
wczytywanie.ValueMember = "name";
wczytywanie.DisplayMember = "name";
This might do
conn = new MySqlConnection(connectString);
conn.Open();
comm = new MySqlCommand(query, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
DataSet DS = new DataSet();
adapter.Fill(DS);
advancedDataGridView1.DataSource = DS.Tables[0];
conn.Close();
And to change the column name your sql be like columnname as 'your preferred column name'
SELECT principal_id as 'Principal ID' from sys.tables where name=#name"
I want to change button text with values get from database unfortunately all buttons take the same text
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\ARANL\\Menu.accdb");
con.Open();
//OleDbDataAdapter da = new OleDbDataAdapter("SELECT Nummer,Speisename FROM Pizza", con); bunun bir anlamı yok.
//DataSet ds = new DataSet(); buna da gerek yok bildiğim kadarıyla.
OleDbCommand kmt = new OleDbCommand("SELECT Nummer,Speisename FROM Pizza", con);// o kodu buraya geçtik.
OleDbDataReader oku = kmt.ExecuteReader();
//da.Fill(ds); buna da
//DataTable dt = new DataTable();
var data = new Dictionary <string, object>();
while (oku.Read())
{
data.Add(oku["Nummer"].ToString(), oku["Speisename"].ToString());
}
con.Close();
button50.Text = data["button50"].ToString();
button51.Text = data["button51"].ToString();
button52.Text = data["button52"].ToString();
button53.Text = data["button53"].ToString();
button54.Text = data["button54"].ToString();
I'm passing a query and parameter from a WinForm to a database class. The
The code on the Form looks like this:
string selectedComp = "CPSI";
string catsQuery = "SELECT id, category, old_value, old_desc, new_value, new_desc, reference1, reference2 FROM masterfiles.xref WHERE company_name = '#company' ORDER BY category, old_value";
Db categoriesData = new Db();
dgvCategories.DataSource = categoriesData.GetData(catsQuery, selectedComp);
And in my database class my code to populate the datatable/set is this:
public DataTable GetData(string selectQuery, string selectedComp)
{
NpgsqlConnection conn = new NpgsqlConnection(connString);
DataSet ds = new DataSet();
NpgsqlCommand cmd = new NpgsqlCommand(selectQuery, conn);
cmd.Parameters.Add(new NpgsqlParameter("#company", selectedComp));
//cmd.Parameters.AddWithValue("#company", selectedComp);
//cmd.Parameters.Add("#company", NpgsqlDbType.Text);
//cmd.Parameters["#company"].Value = selectedComp;
try
{
conn.Open();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);
conn.Close();
da.Fill(ds);
return ds.Tables[0];
}
}
But putting a breakpoint at NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);, selecctQuery hasn't changed - the '#company' is still in the query.
What am I missing?
The root problem is that you're passing the query to the data adapter instead of the command. Change
NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);
to
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
I would also use using to dispose of all objects, and don't close the connection until the dataset is filled:
using(NpgsqlConnection conn = new NpgsqlConnection(connString))
using(NpgsqlCommand cmd = new NpgsqlCommand(selectQuery, conn))
{
cmd.Parameters.Add(new NpgsqlParameter("company", selectedComp));
conn.Open();
using(NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
}
conn.Close();
return ds.Tables[0];
}
i have received this error while doing
the error is in the sqlDa.Fill(dtbl);
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM FeedBackTable WHERE Reviewer = #reviewer", sqlCon);
DataTable dtbl = new DataTable();
//they said the error was at sqlDa.Fill(dtbl);
sqlDa.Fill(dtbl);
gv_feedback.DataSource = dtbl;
gv_feedback.DataBind();
}
You need to fill the #reviewer parameter.
var cmdstr = "SELECT * FROM FeedBackTable WHERE Reviewer = #reviewer";
var cmd = new SqlCommand(cmdstr,con);
cmd.Parameter.add(new SqlParameter("#reviewer", theValueOfTheReviewer);
var sqlDa = new SqlDataAdapter(cmd);
var dtbl =new Datatable();
sqlDa.fill(dtbl);
You didn't add the #reviewer parameter and set its value. Put the value where it says "reviewerIdHere".
DataTable dtbl = new DataTable();
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
SqlDataAdapter sqlDa = new SqlDataAdapter("SELECT * FROM FeedBackTable WHERE Reviewer = #reviewer", sqlCon);
sqlDa.SelectCommand.Parameters.AddWithValue("#reviewer",reviewerIdHere);
//they said the error was at sqlDa.Fill(dtbl);
sqlDa.Fill(dtbl);
}
gv_feedback.DataSource = dtbl;
gv_feedback.DataBind();
SqlConnection con = getConnection();
SqlDataAdapter da = new SqlDataAdapter(storedprocedureName, con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
con.Open();
DataSet DS = new DataSet();
da.Fill(DS);
stateDropDownList.DataSource = DS.Tables[0];
stateDropDownList.ValueMember = "STATE_ID";
stateDropDownList.DisplayMember = "STATE_NAME";
This data source is returning state details and i have attached the satedetails datasource into combobox.
i want to display the combobox with first item select and rest as all states.
i googled it a lot for dropdown list there are codes but nothing showing ...
It can be done in the same way as Drop Down list:
ComboBox1.Items.Insert(0, "--Select--");
Entire code:
SqlConnection con = getConnection();
SqlDataAdapter da = new SqlDataAdapter(storedprocedureName, con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
con.Open();
DataSet DS = new DataSet();
da.Fill(DS);
ComboBox1.DataSource = DS.Tables[0];
ComboBox1.ValueMember = "STATE_ID";
ComboBox1.DisplayMember = "STATE_NAME";
ComboBox1.Items.Insert(0, "--Select--");