I have a gridview. I am fetching data from database between two dates and displaying on it. When i tried to switch between two pages then instead of displaying data of next page it shows complete database data.
here is my code
myConn.Open();
SqlCommand cmd = new SqlCommand(#"select User_id , LoginDate from LoginLog where LoginDate between
('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (GridView1.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
Solved paging issue in gridview
For this i just add single line code just above gridview1.databind();
here is my code...
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
Related
When page_load event is occured i am displaying entire data to the gridview. But initially both From Date and To Date's are empty and if i am trying to switch between pageindex of gridview then it shows above error.
Here is my page_load code...
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Attributes.Add("readonly", "readonly");
TextBox2.Attributes.Add("readonly", "readonly");
myConn.Open();
SqlCommand cmd = new SqlCommand("select User_id, LoginDate from LoginLog", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
myConn.Close();
}
}
Here is my GridView1_PageIndexChanging code...
public void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string ToDate = "";
DateTime dtd = DateTime.Parse(TextBox2.Text);
dtd = dtd.AddDays(1);
ToDate = dtd.ToShortDateString();
myConn.Open();
SqlCommand cmd = new SqlCommand(#"select User_id , LoginDate from LoginLog where LoginDate between
('" + TextBox1.Text + "') and ('" + ToDate + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
myConn.Close();
}
I searched for above query and got so many answers but none of that answer is helpful for me. Hence Posting this question.
I have a gridview with Two dates.
From
To
When I select two dates and if data between two dates is available in database then it will get displayed on gridview. If data is not available then I want to show a Message on Label i.e. "No Records Found"
Here is my code.
myConn.Open();
SqlCommand cmd = new SqlCommand(#"select User_id , LoginDate from LoginLog where LoginDate between ('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (GridView1.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
Add this to your ASPX file :
<asp:GridView ID="GridView1" runat="server" CellPadding="5" BorderStyle="Ridge" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found." EmptyDataRowStyle-ForeColor="Red">
And your CS will be :
myConn.Open();
SqlCommand cmd = new SqlCommand(#"select User_id , LoginDate from LoginLog where LoginDate between
('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
myConn.Close();
Or you can see this link:
Show asp.net Gridview Header when no data/records found
Add this in gridview control
EmptyDataText="No Records Found"
Just check whether DataTable contains values or not.
myConn.Open();
SqlCommand cmd = new SqlCommand(#"select User_id , LoginDate from LoginLog where LoginDate between
('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
hope this helps. Regards :)
Try this method
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
private void Retrieve()
{
if(loadPositions() != null){
dgvEmployeePositions.DataSource = loadPositions();
}
else{
Lab4.Text = "No Records Found ";
}
}
private DataTable loadPositions()
{
DataTable dt = new DataTable();;
myConn.Open();
String q = "your query here";
MySqlCommand cmd = new MySqlCommand(q, connectionString);
MySqlDataReader r = cmd.ExecuteReader();
if(r.hasRows){
dt.Load(r);
return dt;
}
else{
return null;
}
}
How can I search my date into the datagrid view? I have a sample code which I think is correct but why does it not search?
private void textBox3_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox3.Text))
{
SqlDataAdapter sda = new SqlDataAdapter("Select * from STUDENT_RESEARCH_PROJECTS WHERE COURSE LIKE '" + comboBox2.Text + "%'", con);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}
else
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT ACCESSION_NO,TITLE_PROJECT,CATEGORY,YEAR,COURSE,DATE,Student_Name1,Student_Name2,Student_Name3,Student_Name4,Student_Name5,RELATED_TITLE1,RELATED_TITLE2,RELATED_TITLE3,RELATED_TITLE4,RELATED_TITLE5 FROM STUDENT_RESEARCH_PROJECTS WHERE DATE LIKE'" + textBox3.Text + "%'", con);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}
}
You cannot use LIKE for date comparison unless your DATE field is a string
Also you need to check how is the date data saved into database
For example in my database I am using datetime which is being saved like 2018-03-20 07:25:58.557
So when you are querying you need to put a format of your date in order to be accepted
First be aware SQL injection.
you cannot use like operator on Date Column.
Your search TextBox value date format is 03/20/2018(mm/dd/yyyy) and database column contains something different which is usually 2018-03-20 00:00:00.000 to search with the date change the format when querying from database like this.
private void textBox3_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty("textBox3.Text"))
{
string query = "Select * from STUDENT_RESEARCH_PROJECTS WHERE COURSE LIKE #COURSE";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("#COURSE", comboBox2.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}
else
{
string query = "SELECT ACCESSION_NO,TITLE_PROJECT,CATEGORY,YEAR,COURSE,DATE,Student_Name1,Student_Name2,Student_Name3,Student_Name4,Student_Name5,RELATED_TITLE1,RELATED_TITLE2,RELATED_TITLE3,RELATED_TITLE4,RELATED_TITLE5 FROM STUDENT_RESEARCH_PROJECTS WHERE CONVERT(varchar(10) ,DATE, 101) = #DATE";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("#DATE", textBox3.Text);
SqlDataAdapter sda = new SqlDataAdapter();
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
}
}
This is my SEARCH Function with gridview.I'm not able to get gridview in Form remaining all are fine.Here i added new columns two.Please help me out.thanks
private void txtSearch02()
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(myStr);
SqlCommand cmd = new SqlCommand("select ItemCode,ItemName,PointsNeeded from tb_ItemRedemption where (ItemCode is null or ItemCode='" + txtkey2.Text.Trim() + "') or (ItemName is null or ItemName='" + txtkey2.Text.Trim() + "')", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
Session["ItemCode"] = dt;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
dt = ds.Tables[0];
con.Close();
dt.Columns.Add("Quantity");
dt.Columns.Add("TotalPoints");
// for caliculation
txtPointsNeeded.Text = dt.Rows[0]["PointsNeeded"].ToString();
//dt.Rows[0]["Quantity"].ToString();
//dt.Rows[0]["TotalPoints"].ToString();
DataRow dr;
dr = dt.NewRow();
dt.Rows.Add(dr);
// for caliculation
txtGetQuantity.Text = txtQuantity.Text;
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Visible = true;
}
Following on from my comments regarding parameterising the query and using the 'using(){}' statement I'd start by writing something like this:
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(myStr))
{
string sql = "select ItemCode,ItemName,PointsNeeded from tb_ItemRedemption where (ItemCode is null or ItemCode = #txtkey2) or (ItemName is null or ItemName = #txtkey2)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("#txtkey2", txtkey2.Text.Trim());
SqlDataAdapter sda = new SqlDataAdapter(cmd);
//You're currently adding a blank DataTable to a session variable below as you've not
//filled it yet. I've left this incase that's what you meant to do.
Session["ItemCode"] = dt;
con.Open();
//SqlDataAdapter should be able to fill a DataTable as well as a dataset.
sda.Fill(dt);
if (dt.Rows.Count > 0) //Put a breakpoint here to check if anything is returned in dt
{
dt.Columns.Add("Quantity");
dt.Columns.Add("TotalPoints");
// for caliculation
txtPointsNeeded.Text = dt.Rows[0]["PointsNeeded"].ToString();
//Not sure what you're trying to do here. You're just adding a blank row to dt.
DataRow dr;
dr = dt.NewRow();
dt.Rows.Add(dr);
// for caliculation
txtGetQuantity.Text = txtQuantity.Text;
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Visible = true;
}
else
{
//Code to handle nothing being returned.
}
}
If you have previous three columns (with bindvalue) and add other two columns, It would be confusing one for it.
Add all the columns to datatable again. means three and new two.
Try it. It should work.cause your code is right.
Make Sure your Auto Generated Columns is set to 'true'.
Go to properties of grid view, and change auto generated columns to true in case of false.
Go through the links for further clarification : Click here
I have a dropdown list with checkbox in the page and when I select multiple options and search its not returning all the selected values in the grid. It returns only one selected value. Here is the code I use it for search. Any suggestions or change in the code. I have binded one gridview column to that checkbox. Checkbox is present outside the gridview. Any suggestions or change in the code?
<asp:CheckBoxList ID="cblGroup" Style="vertical-align: baseline" runat="server" CssClass="chkbox">
</asp:CheckBoxList>
Below is the code on button search I am trying to fetch the checkbox values
protected void btnSearchGroup_Click(object sender, ImageClickEventArgs e)
{
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select * from AppInvent_Test where Designation= '" + cblGroup.SelectedValue + "'", con);
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
Adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
OK as mentioned by Sudhakar, Try this
protected void btnSearchGroup_Click(object sender, EventArgs e)
{
string selectedValues = string.Empty;
foreach (ListItem item in cblGroup.Items)
{
if (item.Selected)
selectedValues += "'" + item.Value + "',";
}
if (selectedValues != string.Empty)
selectedValues = selectedValues.Remove(selectedValues.Length - 1);//To remove the last comma;
SqlConnection con = new SqlConnection(strcon);
SqlCommand cmd = new SqlCommand("select * from AppInvent_Test where Designation in (" + selectedValues + ")", con);
SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
Adpt.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}