GridView not appearing on webpage - c#

I am building a web form. I have one search field and a search button. I am connecting to MS Access database to retrieve and display the result on the grid view. But my grid view is not appearing on the web page.
Can anyone please help me to find out where I am wrong?
Here is my aspx.cs code:
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Smita\\Desktop\\Project.accdb");
DataTable dt = new DataTable() ;
if (txtMerchant.Text.Length > 0)
{
con.Open();
OleDbDataAdapter DBAdapter = new OleDbDataAdapter();
DBAdapter.SelectCommand = new OleDbCommand("select * from Test where Merchant ID like '" + txtMerchant.Text + "%'", con);
DBAdapter.Fill(dt);
GridView1.DataSource = dt;
}

You have to call the DataBind, binding method first after you assign the data source.
Like that:
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();

GridView1.DataSource = dt; //Assigned a blank table.
"dt" Doesn't seem to point to anything.

Related

c# asp.net Index was out of range. when I trying to select a row

I have a gridview on my page. And I have 2 snippets of SQL code to binds that gridview.
First one is run on page load. If there are records returned from the first SQL, I can select a row on gridview.
But the problem is when there is no record returned from the first SQL, I have button that runs another SQL and binds its result to the gridview too. But when I try to select a row, I get this error:
Index was out of range. when I trying to select a row Must be non-negative and less than the size of the collection. Parameter name: index
My code is like that
First SQL (its run on page load)
void listele()
{
baglanti.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * From kayitlar where durum='Çözülmedi' or durum='İşlem Yapılıyor'", baglanti);
DataTable dataTable = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dataTable);
GridView1.DataSource = dataTable;
GridView1.DataBind();
baglanti.Close();
}
and thats the second SQL that when runs when I click button
void listelehepsi()
{
baglanti.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * From kayitlar", baglanti);
DataTable dataTable = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dataTable);
GridView1.DataSource = dataTable;
GridView1.DataBind();
baglanti.Close();
}
and this is the GridView1_SelectedIndexChanged event
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int secili;
secili = GridView1.SelectedIndex;
GridViewRow row = GridView1.Rows[secili]; // I GOT ERROR HERE
TextBox1.Text = row.Cells[1].Text;
}
why Am I getting this error ?
EDIT--
I got solve changing the page load sql like this;
void listele()
{
baglanti.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * From kayitlar where durum='Çözülmedi' or durum='İşlem Yapılıyor'", baglanti);
DataTable dataTable = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dataTable);
GridView1.DataSource = dataTable;
if (!IsPostBack)
{
GridView1.DataBind();
}
else
{
//
}
baglanti.Close();
}
Make sure that you are not rebinding your datagrid on postback - you can do this in your PageLoad event - something like
if (!IsPostback)
{
... bind your datagrid
}
In the GridView1_SelectedIndexChanged event, could you simply do a RowCount to see if the value is != 0 before the other code runs?
if (GridView1.RowCount <= 0)
{
return;
}

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

Can anyone help me view textbox (multi-line textmode) in gridview with exactly the same format

having a problem again... i found a code, but it doesn't work with me... and can't find any related topic here... so then i ask... can anyone do this?
see this link for the image... Please help me do this...
here is our c# code to add and view it on the GridView asp.net
public partial class parypackage : System.Web.UI.Page
{
SqlConnection PartyConnection = new SqlConnection();
DataSet partyDataSet = new DataSet();
SqlDataAdapter partySqlDataAdapter = new SqlDataAdapter();
private string connect;
protected void Page_Load(object sender, EventArgs e)
{
connect = WebConfigurationManager.ConnectionStrings["PartyConnection"].ConnectionString;
PartyConnection = new SqlConnection(connect);
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
PartyConnection.Open();
SqlCommand cmd = new SqlCommand("select * from package", PartyConnection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
connect = WebConfigurationManager.ConnectionStrings["PartyConnection"].ConnectionString;
PartyConnection = new SqlConnection(connect);
PartyConnection.Open();
SqlCommand addCommand = new SqlCommand("INSERT package (pkgnumber,pkgitems,pkgamount) values ('" + Convert.ToInt32(TextBox1.Text) + "',#Richtextbox ,'" + Convert.ToDouble(TextBox3.Text) + "')", PartyConnection);
SqlDataReader partySqlDataReader;
addCommand.Parameters.AddWithValue("#Richtextbox", TextBox2.Text);
partySqlDataReader = addCommand.ExecuteReader();
partySqlDataReader.Close();
PartyConnection.Close();
BindGridview();
Response.Redirect("partypackage.aspx");
}
}
No, you cant do this with a multi-line textbox. Although you may try some workarounds but it will be too difficult to have the desired result. A textbox captures the plain text inputted to it. Basically, this is the job of text editor to present the user with styling facilities and capturing the data inputted in form of HTML markup. I strongly recommend to use any free text editor instead of a multiline textbox to achieve this easily.

Grid view is not appearing when im running my website

So i have a website hosted on blacknight.com. Im coding with cSharp and asp.net I have mySQL database stored within blacknight also.
However I want to add an admin section to my site where a user could log in and press a "load data" button and the registration table values from blacknight database would appear in a grid view.
However its just not working. Im wondering do I have to physically connect my gridview to my database? Because I have tried to connect to my hosted database with my gridview and it keeps saying it cant connect.
Below is the code behind my load data button. As as it stands when I upload my adminpages to blacknight and run, the gridview is not even appearing. This is my Fourth year project for college and I really need to get it working. Any help would be much appreciative.
protected void Button1_Click(object sender, EventArgs e)
{
string constring ="Server=xxxx; Database=xxxx; Uid=xxx; Pwd=xxx";
MySqlConnection conDb1317466_bk = new MySqlConnection(constring);
DataSet dbdataset = new DataSet();
//binding.DataSource = this.bindingSource.DataSource;
MySqlCommand cmdDb1317466_bk = new MySqlCommand("Select * from db1317466_bk.registration;", conDb1317466_bk);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDb1317466_bk;
DataTable dbdataset1 = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
//DataGridView1.DataSource = bSource;
DataGridView1.DataBind();
sda.Update(dbdataset);
}
catch (Exception)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptkey", "<script>alert('Can Load data');</script>");
}
}
}
Try this one
GridView1.DataSource=dbdataset;
GridView1.DataBind();
Instead of using
BindingSource bSource = new BindingSource();
bSource.DataSource = dbdataset;
DataGridView1.DataBind();
Also, I don't think that you should use:
sda.Update(dbdataset);
Try this
try
{
MySqlDataAdapter da = new MySqlDataAdapter();
da.SelectCommand = cmdDb1317466_bk;
DataTable dt= new DataTable();
da.Fill(dt);
DataGridView1.DataSource = dt;
DataGridView1.DataBind();
}

How to select data from table selected in dropdownlist?

I have a dropdownlist on a page and in a dropdownlist I have names of the tables in database.
I don't know how to insert data from the selected table in the gridview or datalist.
I tried with using the code from another c# project I did in school, but it didn't work
edit: answer added here
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(connectionString);
OleDbDataAdapter da = new OleDbDataAdapter (string.Format("SELECT * from {0}", dropDownList.SelectedValue), con);
DataSet ds = new DataSet();
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
con.Close();
}
You can tell the code to put the dropdowlists selected item's value in the query as follows:
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection con= new OleDbConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(string.Format("SELECT * FROM {0}",DropDownList1.SelectedValue), con);
DataSet ds = new DataSet();
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
con.Close();
}
I've tested this before posting it and it works on my machine. When you so if you can not get it worked the problem should be somewhere else.
That is very easy to do. I assume your dropdownlist is in your gridview which is also a asp gridview. If this is the case then in the event you have catching the selectedindexchange or by button click. You will have to find the dropdown in the gridview and then use the selectedvalue property. It will look like this to find it(place this in your event handler)
Dim ddl as dropdownlist = gridview.row(e.rowindex).findcontrol("dropdownID")
Dim val = ddl.selectedvalue
The only changes that might have to be made in regards to finding the control is depending on what rowtype the dropdown is in and also the event being used. If the above wont find the control you can try gridview.row.findcontrol("dropdownID")
Once you get the value, you can call the method for the insert and pass it as a parameter :)

Categories