So here's whats happening. when I press the event button1 to retrieve the data from my database it shows System.Data.DataRowView. But when I press the button1 event again it shows the actual result and correct data. I want to know how to fix this in just one click of a button to display actual data
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"SQLCONNECTIONSTRING");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from tbl_members", con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("FullName", typeof(string));
dt.Load(reader);
DataSet ds = new DataSet();
adapter.Fill(ds);
checkedListBox1.DisplayMember = "FullName";
checkedListBox1.ValueMember = "FullName";
checkedListBox1.DataSource = ds.Tables[0];
checkedListBox1.Enabled = true;
checkedListBox1.Refresh();
con.Close();
}
try this.. You don't need to execute the query for twice.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"SQLCONNECTIONSTRING");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from tbl_members", con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
//SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("FullName", typeof(string));
DataSet ds = new DataSet();
adapter.Fill(ds);
dt=ds.Tables[0];
checkedListBox1.DisplayMember = "FullName";
checkedListBox1.ValueMember = "FullName";
checkedListBox1.DataSource = ds.Tables[0];
checkedListBox1.Enabled = true;
checkedListBox1.Refresh();
con.Close();
}
Shouldn't the order of assignment be different?
checkedListBox1.DataSource = ds.Tables[0];
checkedListBox1.DisplayMember = "FullName";
checkedListBox1.ValueMember = "FullName";
instead of
checkedListBox1.DisplayMember = "FullName";
checkedListBox1.ValueMember = "FullName";
checkedListBox1.DataSource = ds.Tables[0];
Related
I do not get any error messages, but the Datagrid won't show any Data from my Database.
var con = new MySqlConnection("server= localhost; database=fußballmanager; Uid=root;Pwd=;");
private void Form4_Load(object sender, EventArgs e)
{
MySqlDataAdapter da = new MySqlDataAdapter("Select * from tbl_anmeldedaten", con);
da.SelectCommand.CommandType = CommandType.Text;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
I got this error "dynamic sql generation is not supported against multiple base tables" in my project.
i got the error on "da.update(dt)" line.
this is the code which i got the error...`so help me how can i solve it..
OleDbDataAdapter da;
DataSet ds = null;
BindingSource bsource = new BindingSource();
OleDbCommand cmd;
public void BindData()
{
cmd = new OleDbCommand("select t.srno,c.AccNumber2,c.Name,t.Admission_Fee,t.Family_fund,t.MonthlyCollection,t.MonthlyDeposit from transDemand t inner join Customer c on t.AccNumber=c.AccNumber where t.IssueDate=#IssueDate and c.Dismember=false", con);
cmd.Parameters.Add("#IssueDate", OleDbType.Date).Value = datesearch.Value.ToString("dd-MM-yyyy");
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
//dt = new DataTable();
//da.Fill(dt);
OleDbCommandBuilder cmdbuild = new OleDbCommandBuilder(da);
da.Fill(ds, "A");
bsource.DataSource = ds.Tables["A"];
dataGridView1.DataSource = bsource;
}
private void btnupdate_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["A"];
this.dataGridView1.BindingContext[dt].EndCurrentEdit();
da.Update(dt);
BindData();
}
I have datagridview, that i must fill by 5 tables. I declared SqlCommand and SqlConnection.
After that I use somethine like this:
selCommand.Connection = conn;
dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = selCommand;
ad.Fill(dt);
dataGridView1.DataSource = dt;
As a result I have column headers of my query in datagridview, but don't have data.
I tried use this code:
selCommand.Connection = conn;
dt = new DataTable();
SqlDataReader dr = selCommand.ExecuteReader();
dt.Load(dr);
bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
dr.Close();
It was working, but I something change and I can't understand why it does not work.
Try this:
DataTable table = null;
using (SqlConnection connection = new SqlConnection(this.connectionString))
{
try
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM Something WHERE Id = #Id";
cmd.Parameters.Add(new SqlParameter("#Id", YourValue));
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
table = new DataTable();
adapter.Fill(table);
}
}
catch (Exception ex)
{
//Handle your exception;
}
}
dataGridView1.DataSource = table;
I have a windows application C#
Where i have a datagridview1 where i need to retrieve a multitable data query
When the data is being retrieved from the table,it need to be filtered also at the same time
The problem that i am facing that the data is not been loaded although in the LocalWindows shows
The problem is The data is not being loaded in the datagridview
I can see the data is being loaded in the datagridview through the autos watch in visual studio-i have given the output in the end
what did i do wrong ?
//CODE
private void getData(string selectquery)
{
try
{
// string qryText1 = #"SELECT FEE_HEAD.FEE_HEAD_NAME, FEE_AMOUNT.FEE_HEAD_AMOUNT, FEE_AMOUNT.CLASS_ID FROM FEE_AMOUNT INNER JOIN FEE_HEAD ON FEE_AMOUNT.FEE_HEAD_ID = FEE_HEAD.ID";
SqlConnection con = new SqlConnection(#"Data Source=SRINATH-PC\SQLEXPRESS;Initial Catalog=BFMS;Integrated Security=True");
SqlCommand command = new SqlCommand(selectquery, con);
con.Open();
SqlDataAdapter dataAdapter1 = new SqlDataAdapter();
dataAdapter1.SelectCommand = command;
DataTable dataT = new DataTable();
dataAdapter1.Fill(dataT);
BindingSource bs = new BindingSource();
bs.DataSource = dataT;
dataGridView1.DataSource = bs;
dataAdapter1.Update(dataT);
dataGridView1.Refresh();
con.Close();
}
catch (SqlException ex1)
{
MessageBox.Show(ex1.ToString());
}
}
//LOCAL WATCH VIEW
Count 28 int+ Current {System.Data.DataRowView} object {System.Data.DataRowView}
DataMember "" string
RowCount 29 int
RowHeadersBorderStyle Raised System.Windows.Forms.DataGridViewHeaderBorderStyle
the code is working perfectly
public Form1()
{
InitializeComponent();
string query = "select * from benefitplans";
getData(query);
}
and then
private void getData(string selectquery)
{
try
{
// string qryText1 = #"SELECT FEE_HEAD.FEE_HEAD_NAME, FEE_AMOUNT.FEE_HEAD_AMOUNT, FEE_AMOUNT.CLASS_ID FROM FEE_AMOUNT INNER JOIN FEE_HEAD ON FEE_AMOUNT.FEE_HEAD_ID = FEE_HEAD.ID";
SqlConnection con = new SqlConnection(#"Data Source=392;User id=sa; Password=manage#123;Initial Catalog=Benefit;Pooling=False");
SqlCommand command = new SqlCommand(selectquery, con);
con.Open();
SqlDataAdapter dataAdapter1 = new SqlDataAdapter();
dataAdapter1.SelectCommand = command;
DataTable dataT = new DataTable();
dataAdapter1.Fill(dataT);
BindingSource bs = new BindingSource();
bs.DataSource = dataT;
dataGridView1.DataSource = bs;
dataAdapter1.Update(dataT);
dataGridView1.Refresh();
con.Close();
}
catch (SqlException ex1)
{
MessageBox.Show(ex1.ToString());
}
}
Try replacing these lines
SqlDataAdapter dataAdapter1 = new SqlDataAdapter();
dataAdapter1.SelectCommand = command;
DataTable dataT = new DataTable();
dataAdapter1.Fill(dataT);
BindingSource bs = new BindingSource();
bs.DataSource = dataT;
dataGridView1.DataSource = bs;
dataAdapter1.Update(dataT);
dataGridView1.Refresh();
con.Close();
with
SqlDataAdapter dataAdapter1 = new SqlDataAdapter();
dataAdapter1.SelectCommand = command;
DataSet dataS = new DataSet();
dataAdapter1.Fill(dataS);
dataGridView1.DataSource = dataS;
con.Close();
Hope, this may help.
We need to use display the Columns for the Datagridview using
dataGridView1.Columns[0].Name = "FEE_HEAD_NAME";
ataGridView1.Columns[0].HeaderText = "FEE_HEAD_NAME";
dataGridView1.Columns[0].DataPropertyName = "FEE_HEAD_NAME";
dataGridView1.Columns[1].HeaderText = "FEE_HEAD_AMOUNT";
dataGridView1.Columns[1].Name = "FEE_HEAD_AMOUNT";
dataGridView1.Columns[1].DataPropertyName = "FEE_HEAD_AMOUNT";
Anyways Thanks for the reply everyone
I have used DataRow which could be displayed in a combobox in my WinForm but somehow, it is not working, although the entire table is successfully being displayed in the gridview.
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
con.ConnectionString = "Data Source=.;Initial Catalog=StudentDetails;Integrated Security=True";
cmd.Connection = con;
cmd.CommandText = "select * from StuDet";
da.SelectCommand = cmd;
da.Fill(ds);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
DataRow dr = dt.NewRow();
for (int i = 0; i < dt.Rows.Count; i++)
{
comboBox1.Items.Add(dr[0]);
}
}
You create an empty new row
DataRow dr = dt.NewRow();
and add it Rows.Count times to comboBox1. I think what you want is to go through each row in dt.Rows:
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["Roll"]);
}
Just now figured out the answer, Please take a look at the loop at the bottom.
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
con.ConnectionString = "Data Source=.;Initial Catalog=StudentDetails;Integrated Security=True";
cmd.Connection = con;
cmd.CommandText = "select * from studet";
da.SelectCommand = cmd;
da.Fill(ds);
dt = ds.Tables[0];
dataGridView1.DataSource = dt;
////////////////////////////////////////////////////
DataRow dr = dt.NewRow();
for (int i = 0; i < dt.Rows.Count; i++)
{
dr[0] = dt.Rows[i][0];
comboBox1.Items.Add(dr[0]);
}
}