i have a gridview where i have allowed paging.
but when i click second page the gridview disappears
here is the c# code :
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
OdbcDataAdapter adpState = new OdbcDataAdapter("SELECT CALL_NO,TDATE,
ID_NO,NAME,CONTACT,DEPARTMENT,ISSUE,STATUS FROM TBL_ITHELPDESK
WHERE (STATUS IS NULL OR STATUS <> 'CLOSED') AND TDATE= TO_DATE('" +
txtDate.Text.ToString().Trim() + "','MM-DD-YYYY')", con1);
DataSet ds = new DataSet();
adpState.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
can anyone help to find where i'm going wrong
You have to check multiple things.
Check the query with the one that is working, Is it Same?
Check the con1 variable is defined outside of any other method.
Define con1 in a class not in a method, something like that
odbcConnection con = new odbcConnection(ConectionString);
Related
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.
I'm doing a filter form by 3 criteria, I'm having problems on the pagination since I do not load the data from the second page of the GridView, apparently I have to call my function that fills the GridView in the Load page event, but this function receives parameters that are the ones that the user enters in the search filter, How can I then call the function sending the parameters to that function in the page load event every time the page is changed in the GridView?
function
public DataTable AdvertSearch(string tittle, DateTime datel, DateTime date2)
{
SqlConnection con = new SqlConnection(Util.GetConnectionString("ConnectionString"));
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select*from table where titAdvert=#tittle or dateStart=#dateS" or dateEnd=#dateE;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#tittle", tittle== "" ? (object)DBNull.Value : tittle);
cmd.Parameters.AddWithValue("#dateS", date1== "" ? (object)DBNull.Value : date1);
cmd.Parameters.AddWithValue("#dateE", date2== "" ? (object)DBNull.Value : date2);
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
search button
protected void BtnSearch_Click(object sender, EventArgs e)
{
GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
GridView1.DataBind();
}
You need to specify a pageIndexChanging event to trigger the grid to be reloaded when the user clicks to the next page. Check out the code below:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
GridView1.DataBind();
}
And please write your code in english, as it makes it more readable for all programmers.
I am currently attempting to make gridview load 1 question at a time, when I set the paging to 1 it gives me this error.
Also using the VS15 SQL server
Try using an SqlAdapter instead of SqlDataReader. The reason is because you cannot Page back if you use SqlDataReader. SqlAdapter supports bidirectional traversal.
You were not using a Data table to retrieve the results form the select query. Use this instead
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
loadgrid();
}
}
private void loadgrid() {
con.Open();
cmd.CommandText = "SELECT TOP 4 * FROM [Question] ORDER BY NEWID()";
cmd.Connection = con;
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
ad.Fill(dt);
GridView1.DataSource = dt;
GridView1.AllowPaging = true;
GridView1.DataBind();
}
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.
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 :)