Show cart details on a button click event - c#

I want to show my cart details on a Button_Click_Event. I'm using a Gridview control of ASP.NET framework. But the following exception occurs:
object reference is not set to a instance of an object.
How can I fix it? Here is my code:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string st = "select * FROM cart_master";
cmd = new SqlCommand(st, sqlcon);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView GridView1 = (GridView)ContentPlaceHolder1.FindControl("GridView1");
sda.Fill(ds, "cart_master");
GridView1.DataSource = ds.Tables["cart_master"];
GridView1.DataBind();
cmd.Connection.Close();
}

Rather than dataset, just fill a datatable from your adapter
DataTable dataTable = new DataTable("ResultDataTable");
sda.Fill( dataTable);
GridView1.DataSource = dataTable;

Related

I cannot get the Data out of my MySql Database into my DataGridView

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;
}

how to solve "dynamic sql generation is not supported against multiple base tables" Error?

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();
}

Could not show content of a table in SQL server to C# Gridview

I want to show content of a table("newexample") in my database in SQL server to gridview in ASP.net, but the gridview shows nothing. How to fix it?
Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string cs = ConfigurationManager.ConnectionStrings["SampleDBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from newexample", con);
DataTable dt = new DataTable();
dt.Columns.Add("col0");
dt.Columns.Add("col1");
dt.Columns.Add("col2");
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
DataRow dr = dt.NewRow();
dr["col0"] = rdr["col0"];
dr["col1"] = rdr["col1"];
dr["col2"] = rdr["col2"];
dt.Rows.Add(dr);
}
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
Did you get any exception? Try moving con.Close() after DataBind() and see how it goes.
Try to use the code below:
DataSet dataSet = new DataSet("newexample");
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from newexample", connectionString);
dataAdapter.Fill(dataSet);
if (dataSet != null)
{
if (dataSet.Tables[0].Rows.Count != 0)
{
GridView1.DataSource = dataSet ;
GridView1.DataBind();
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
}
}
This is a better approach since SqlDataAdapter will take care for SqlConnection. It is responsible to create the connection and to close it at the end of operation in the best way possible.
This is not a tested code so if you have any problem let me know..

How can I display the latest value in Chart?

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);

Having Problems populating my checkedListBox

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];

Categories

Resources