I have to display chart that is connected with db dynamically. It is working fine. The code is given below. But my issue is my db is automatically update in every 5 minutes. So I have to display the chart with latest inserted information. Please help me.
The code is given below:
public partial class chartDummy : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(#"ConnectionString");
cmd = new SqlCommand("Select Mains_Run_Hrs, DG_Run_Auto_Mode, Battery_Run_Hrs, Solar_Run_hrs from tbl_runtime_report", con);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
DataView source = new DataView(ds.Tables[0]);
Chart1.DataSource = ds;
Chart1.Series[0].YValueMembers = "Mains_Run_Hrs";
Chart1.Series[0].XValueMember = "DG_Run_Auto_Mode";
Chart1.Series[0].XValueMember = "Battery_Run_Hrs";
Chart1.Series[0].XValueMember = "Solar_Run_hrs";
Chart1.DataBind();
}
}
cmd = new SqlCommand("Select Mains_Run_Hrs, DG_Run_Auto_Mode, Battery_Run_Hrs, Solar_Run_hrs from tbl_runtime_report ORDER BY Site_ID DESC", con);
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 am trying to change value of a TextBox control after the value of ComboBox changes. My code returns System.Data.DataRowView on the first time ComboBox value changes.
That is why I am getting an Object instead of the actual value on first call to my combobox.
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionString"].ConnectionString);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd= new SqlCommand("SELECT donor_name,ID FROM donor_detail",con);
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
DonorName.DataSource = dt;
DonorName.DisplayMember = "donor_name";
DonorName.ValueMember = "ID";
con.Close();
}
And this is Event of change Value of ComboBox which is supposed to change value of my TextBox also when value in ComboBox changes.
private void DonorName_SelectionChangeCommitted(object sender, EventArgs e)
{
var id = Convert.ToString(DonorName.SelectedValue);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionString"].ConnectionString);
con.Open();
String sql = "SELECT * from donor_detail WHERE ID=" + id + "";
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
MessageBox.Show(sql);
mobile.Text = dt.Rows[0]["mobile"].ToString();
con.Close();
}
As mentioned here and several other places, this happens if you don't specify the datasource after the display and value members. This is the correct order:
DonorName.DisplayMember = "donor_name";
DonorName.ValueMember = "ID";
DonorName.DataSource = dt;
I'm trying to make a database search in my app where the user would choose the column and enter the search word and the result would come up in a dataviewgrid.
This is the code i've been working on, the problem is that nothing comes up and i'm pretty sure there are entries in the database. EDIT : it's a windows form application
private void button1_Click(object sender, EventArgs e)
{
conn = new SqlConnection("Server = localhost; database = Clients; Integrated Security = SSPI");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * From dbo.Tclients WHERE #choice = #input", conn);
cmd.Parameters.AddWithValue("#choice", comboBox1.Text);
cmd.Parameters.AddWithValue("#input", textBox1.Text);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
You cannot use a parameter to express the name of a column.
You should populate your combobox with the column names and set its DropDownStyle property to DropDownList (do not allow your user to type the name of the column) and then build your query
private void button1_Click(object sender, EventArgs e)
{
string cmdText = "SELECT * From dbo.Tclients WHERE " + comboBox1.Text + " = #input";
using(SqlConnection conn = new SqlConnection(....))
using(SqlCommand cmd = new SqlCommand(cmdText, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("#input", textBox1.Text);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
}
you forgot to bind the grid view with datasource
add this after data source
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.DataBind();
private void bunifuThinButton21_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = (#"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\albasheer\Desktop\games\my_school\my_school\school.mdf;Integrated Security=True;User Instance=True");
connection.Open();
string sql = "select name,id,stage,age,cost from STUDENT where stage like '%" + bunifuCustomLabel1.Text + "%' and name like '%" + bunifuMaterialTextbox1.Text + "%'";
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
SqlCommand command = new SqlCommand(sql, connection);
DataTable table = new DataTable();
adapter.Fill(table);
var dt = from t in table.AsEnumerable()
select new
{
id = t.Field<int>("id"),
Name = t.Field<string>("name"),
};
bunifuCustomDataGrid1.DataSource = dt.ToList();
}
So here I'm trying to populate my dropdown list, the code behind is as below:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection));
con.Open();
SqlCommand mycommand = new SqlCommand("select * from MSUnit", con);
SqlDataReader ddlvalues = mycommand.ExecuteReader();
ddlTransactionCategory.DataSource = ddlvalues;
ddlTransactionCategory.DataTextField = "categoryCode";
ddlTransactionCategory.DataValueField = "OrgID";
ddlTransactionCategory.DataBind();
mycommand.Connection.Close();
mycommand.Connection.Dispose();
}
the problem is, I can't seem to get it to work, any help? and is this code doing it right?
plz try below code:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection));
con.Open();
SqlCommand mycommand = new SqlCommand("select * from MSUnit", con);
SqlDataAdapter adp =new SqlDataAdapter(mycommand);
DataSet ds =new DataSet();
adp.Fill(ds);
ddlTransactionCategory.DataSource = ds;
ddlTransactionCategory.DataTextField = "categoryCode";
ddlTransactionCategory.DataValueField = "OrgID";
ddlTransactionCategory.DataBind();
mycommand.Connection.Close();
mycommand.Connection.Dispose();
}
Thanks,
Hitesh
Can't bind to a SqlDataReader (or at least I've never tried it). Get a DataTable or a DataSet instead, fill it and then bind that to the dropdown the same way.
Use SqlDataAdataper like the one below
using (SqlConnection con = new SqlConnection(CommonFunctions.GetAppDBConnection(Constants.AppID, Constants.TMDDBConnection)))
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from MSUnit", con);
DataTable dt = new DataTable
da.Fill(dt)
ddlTransactionCategory.DataSource = dt;
ddlTransactionCategory.DataTextField = "categoryCode";
ddlTransactionCategory.DataValueField = "OrgID";
ddlTransactionCategory.DataBind();
}
if you want to use DataReader you must insert one-by-one.
while (ddlvalues.Read())
{
ddlTransactionCategory.Items.Add(new ListItem(ddlvalues.getString("OrgID"),ddlvalues.getString("categoryCode")))
}