Gridview display - c#

I dont know what it is but i'm having all sorts of issues with this gridview. Below is the code but the issues is the grid is not displaying. Visibility is set to true and the query does return results. So I'm asking for another set of eyes to point out what went wrong here.
Thank you
protected void btnDisplay_Click(object sender, EventArgs e)
{
string connString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\levels.mdb";
DataSet ds;
using (OleDbConnection myConnString = new OleDbConnection())
{
myConnString.ConnectionString = connString;
using (OleDbCommand selectCommand = new OleDbCommand())
{
selectCommand.CommandText = "select * from tblTest";
selectCommand.Connection = myConnString;
myConnString.Open();
using(OleDbDataAdapter da = new OleDbDataAdapter())
{
da.SelectCommand = selectCommand;
ds = new DataSet();
da.Fill(ds, "test");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}//end click event
and the gridview
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

or
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

data source should have been:
GridView1.DataSource = ds.Tables["test"];
GridView1.DataBind();

Related

How to load Drop down list values from database while Row Editing in GridView?

I tried the following way, the data set is coming and table is binding.
But data is not inserting to the assigned drop down list in the gridview while Row Editing event
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
int index = e.NewEditIndex;
DropDownList ddl = (DropDownList)GridView2.Rows[index].FindControl("Receipttypeddl");
if (ddl != null)
{
DataSet ds = new DataSet();
SqlDataAdapter da;
con.Open();
string qry;
qry = "select * from ReceiptType";
SqlCommand cmd = new SqlCommand(qry);
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
ddl.DataSource = ds;
ddl.DataTextField = "Receiptmode";
ddl.DataValueField = "Receiptmode";
ddl.DataBind();
ListItem i = new ListItem("", "");//Data is not inserting into ddl
ddl.Items.Insert(0, i);
}
DataTable dts = new DataTable();
dts = (DataTable)ViewState["Receiptdetails"];
GridView2.DataSource = dts;
GridView2.DataBind();
}
you need bind dropdown on RowDataBound.
there seems to be no need to go to dB for each item.
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
BindGridView();
}
private void BindGridView()
{
DataTable dts = new DataTable();
dts = (DataTable)ViewState["Receiptdetails"];
GridView2.DataSource = dts;
GridView2.DataBind();
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("Receipttypeddl");
BindDropdown(ddl);
}
}
DataTable ds = new DataTable();
private void BindDropdown(DropDownList ddl)
{
if (ds.Rows.Count == 0)
{
SqlDataAdapter da;
con.Open();
string qry;
qry = "select * from ReceiptType";
SqlCommand cmd = new SqlCommand(qry);
cmd.Connection = con;
da = new SqlDataAdapter(cmd);
da.Fill(ds);
con.Close();
}
ddl.DataSource = ds;
ddl.DataTextField = "Receiptmode";
ddl.DataValueField = "Receiptmode";
ddl.DataBind();
ListItem i = new ListItem("", "");//Data is not inserting into ddl
ddl.Items.Insert(0, i);
}

How to display data from one page to another in gridview on button click by using session on asp.net

Thanx in advance.
I'm facing issue in transfering data from gridview of home page to another search reasult page. page showing blank only.no data displaying.
Im using with master page.
Im trying to fetch data from textbox for search result like source and destination from textbox.
for refernce please find below code of home.aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=IT_APPS_SUPP;Initial Catalog=dotnet;Integrated Security=True; MultipleActiveResultSets=true");
con.Open();
string str1 = "Select * from busbooking where zone='" + txtSourceBus.Text + "' " + "and destination='" + txtDestBus.Text + "'";
SqlCommand cmd1 = new SqlCommand(str1, con);
cmd1.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(str1, con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Label1.Text = "";
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
GridView1.Visible = true;
dr.Close();
}
else
{
GridView1.Visible = true;
Label1.Text = "Data not found";
}
DataTable dt = GridView1.DataSource as DataTable;//set the datasource
Session["GridData"] = dt;
Response.Redirect("~/BusSearch.aspx",true);
}
=========================================================
bussearch.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["GridData"] != null)
{
DataTable dt = (DataTable)Session["GridData"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
==================================================================
can anyone help for this., my second page showing blank only.
Try passing DataSet instead of DataTable like this
Session["GridData"] = ds.Tables[0];
All the other code can stay as it is.
Hello you can use like this
In bussearch.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["GridData"] != null)
{
DataTable dt = new DataTable();
dt = Session["GridData"] as DataTable;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}

Datagrid not showing

My data grid is not showing up in my webpage at all.As in no table showing up.
I did follow this stack question and modified some of its code to suit mine,but it's not showing at all.
here is the code for my class which is triggered by the button.
private void LoadDataGrid()
{
con.Open();
cmd = new SqlCommand(#"SELECT quotationID,quo_product
FROM JobQuotations
WHERE quo_custname = #custname", con);
cmd.Parameters.AddWithValue("#custname",lblLoginName.Text);
da = new SqlDataAdapter(cmd);
dt = new DataTable();
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
and I have inserted it inside the page_load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDataGrid();
}
}
here is the markup:
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
You are missing da.Fill(dt);:
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;

Gridview with DDl selected value

I had gridview which in load it will get data from database .And I added option for user to filter this grid view by DDl I did my code and the grid get data when load but when I selected DDl it didnot get any data and I made break point I noticed that Gridview1.Databind() hadnot any action on grid.So please any one help me
protected void Page_Load(object sender, EventArgs e)
{
DataTable DT = new DataTable();
if (DDlCity.SelectedIndex<0)
{
using (SqlConnection con = Connection.GetConnection())
{
SqlCommand Com = new SqlCommand("GetDealers", con);
Com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter DA = new SqlDataAdapter(Com);
DA.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
}
}
protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable DT = new DataTable();
using (SqlConnection con = Connection.GetConnection())
{
SqlCommand Com = new SqlCommand("GetDealersByArea", con);
Com.CommandType = CommandType.StoredProcedure;
Com.Parameters.Add(Parameter.NewInt("#DEALERAREA_ID", DDlCity.SelectedValue));
SqlDataAdapter DA = new SqlDataAdapter(Com);
DA.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
}
i suppose you got confused on what i said...no worries
here is a working example of you give example code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridFunction();
}
}
private void BindGridFunction()
{
try
{
DataTable DT = new DataTable();
using (SqlConnection con = Connection.GetConnection())
{
if(DDlCity.SelectedIndex <0)
{
SqlCommand Com = new SqlCommand("GetDealers", con);
Com.CommandType = CommandType.StoredProcedure;
}
else
{
SqlCommand Com = new SqlCommand("GetDealersByArea", con);
Com.CommandType = CommandType.StoredProcedure;
Com.Parameters.Add(Parameter.NewInt("#DEALERAREA_ID", DDlCity.SelectedItem.Value));
}
SqlDataAdapter DA = new SqlDataAdapter(Com);
DA.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
}
catch(Exception ex)
{
DT = null; // etc...etc.. clear objects created
}
}
protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
BindGridFunction();
}
I hope you get what i was trying to say. You can change the code according to you need.
Not tested yet but m sure will work.
Note : i woud suggest to use "DDlCity.SelectedItem.Value" instead of " DDlCity.SelectedValue"
In your post back you can put the binding code in condition
if (!IsPostBack){
// Bind grid here looking for or used call to function something like BindGrid()
}
and in BindGrid() function you can write binding code for grid view.
and on ddl selected index changed event you can again call the BindGrid() method to bind again accordingly.
also check that in your dropdownlist you have EnablePostBack - true.

ASP.NET DropDownList Issue

[...]
public DataSet ReturnPromoMagazinesDs()
{
MySql.Data.MySqlClient.MySqlConnection mysqlConnection = new MySql.Data.MySqlClient.MySqlConnection(this.connectionString);
MySql.Data.MySqlClient.MySqlCommand mysqlCommand = new MySql.Data.MySqlClient.MySqlCommand("SELECT id, magazine_name FROM `magazines`", mysqlConnection);
MySql.Data.MySqlClient.MySqlDataAdapter mysqlAdaptor = new MySql.Data.MySqlClient.MySqlDataAdapter(mysqlCommand);
DataSet ds = new DataSet();
mysqlAdaptor.Fill(ds, "magazines");
return ds;
}
[...]
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = coreObject.ReturnPromoMagazinesDs();
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = ds.Tables["magazines"].Columns["magazine_name"].ColumnName;
DropDownList1.DataValueField = ds.Tables["magazines"].Columns["id"].ColumnName;
DropDownList1.DataBind();
}
[...]
<asp:dropdownlist id="DropDownList1" runat="server"></asp:dropdownlist>
The above code works ok, untill I'm fetching the DropDownList1.SelectedValue which is always 1 (the fist value from the table). Values in the table aren't the blame for this, and if I manually add items to the DropDownList, everything works fine. What can cause this?
Does it work properly when you wrap all of the code in the Page_Load method in the following:
if (!IsPostBack) { /* Code as in the original post */ }
Not sure if something is getting lost in transalation but you could simplify the following lines, even if its doesnt fix the issue.
DropDownList1.DataTextField = "magazine_name";
DropDownList1.DataValueField = "id";

Categories