how to display no records found message in Gridview - c#

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

Related

c# paging issue in gridview while switching to the next page

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

How to retrieve image from database and paste to picturebox?

Any help is much appreciated, guys. I tried using the dataset and other solutions to no avail. The error I keep getting is "Parameter is not valid." My code is this:
try
{
conn.Open();
MySqlCommand comm = new MySqlCommand("SELECT lastname, firstname, picture FROM casestudyprofile WHERE caseid = " + id, conn);
MySqlDataAdapter adp = new MySqlDataAdapter(comm);
DataTable dt = new DataTable();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
lblname.Text = dt.Rows[0]["firstname"].ToString() + " " + dt.Rows[0]["lastname"].ToString();
pbox2.Image = Image.FromStream(new MemoryStream((byte[])dt.Rows[0]["picture"]));
}
conn.Close();
}

How to write code for compare the value in textbox and column in SQL Server?

I want to write code to compare the value for Serial number and equipment id with the column in SQL Server. If the value that user search have been deleted or remove I want to show " no record found".
My problem here is I do not know how to write the code for if statement.
Here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=5CG50749V3\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("Select * FROM [Equipment] WHERE SerialNumber='" + SerialNumber.Text + "' or EquipmentID ='" + EquipmentID.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
SerialNumber.Text = dt.Rows[0][5].ToString();
EquipmentID.Text = dt.Rows[0][4].ToString();
}
it will show a popup if the record is not found
SqlConnection con = new SqlConnection("Data Source=5CG50749V3\\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("Select * FROM [Equipment] WHERE SerialNumber='" + SerialNumber.Text + "' or EquipmentID ='" + EquipmentID.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count != 0) // this will check weather any records are returned
{
// If record is found
SerialNumber.Text = dt.Rows[0][5].ToString();
EquipmentID.Text = dt.Rows[0][4].ToString();
}
else //if no record is found it will display alert
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('no record found');", true);
}
You can use the rows.count of the datatable (dt)
If (dt.rows.count == 0)
{
' no rows returned, notify user
}
give it a try
if (dt != null && dt.Rows.Count >0)
{
//Then your logic
}
else
{
//popup script
}

Winform DataGridView still shows data after final update

I'm developing a winform application using C#.net, using DataGridView to display the data, but after the last update, the DataGridView returns 1 last row, but i want my DataGridView to return a blank row or just show the header only...
nb: Before the update JmlKirimK value is 1.
try
{
con.Close();
con.Open();
MySqlCommand cmd = new MySqlCommand("Select PasienID, NoHP, JmlKirimK from datapasien where JmlKirimK ='1' AND StatusDiabetes='Ya'", con);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
con.Close();
con.Open();
nohp = Convert.ToString(dr[1]);
idpasien = Convert.ToString(dr[0]);
jml = Convert.ToString(dr[2]);
Cursor.Current = Cursors.WaitCursor;
if (objclsSMS.sendMsg(this.port2, nohp, this.txtisi.Text))
{
MySqlCommand cmd3 = new MySqlCommand("UPDATE datapasien SET JmlKirimK='2' Where PasienID='" + idpasien.Trim() + "'", con);
cmd3.ExecuteNonQuery();
terkirim = terkirim + 1;
showgridkirimulang();
}
else
{
tdkterkiri = tdkterkiri + 1;
}
showgridkirimreset();
}
con.Close();
alert.ContentImage = SmsGatewayProlanis.Properties.Resources.Warning1;
alert.CaptionText = "Status";
alert.ContentText = "Pesan Terkirim = " + terkirim + ", Pesan Tidak Terkirim = " + tdkterkiri;
alert.Show();
}
catch (Exception ex)
{
alert.ContentImage = SmsGatewayProlanis.Properties.Resources.Warning1;
alert.CaptionText = "Status";
alert.ContentText = ex.Message;
alert.Show();
ErrorLog(ex.Message);
}
This is my DataGridView code
private void showgridkirimulang()
{
con.Close();
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT NamaPasien, RiwayatPenyakit, AlamatPasien, UmurPasien, Jeniskelamin, NoHP, JmlKirimK, StatusDiabetes FROM datapasien WHERE JmlKirimK=1 AND StatusDiabetes='Ya'", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.ExecuteNonQuery();
if (ds.Tables[0].Rows.Count > 0)
{
gridkirimulang.DataSource = ds.Tables[0];
gridkirimulang.Columns[0].HeaderText = "Nama Pasien";
gridkirimulang.Columns[1].HeaderText = "Riwayat Penyakit";
gridkirimulang.Columns[2].HeaderText = "Alamat Pasien";
gridkirimulang.Columns[3].HeaderText = "Umur Pasien";
gridkirimulang.Columns[4].HeaderText = "Jenis Kelamin";
gridkirimulang.Columns[5].HeaderText = "No. HP";
gridkirimulang.Columns[6].HeaderText = "Jumlah Pesan Terkirim";
gridkirimulang.Columns[7].HeaderText = "Status Diabetes";
gridkirimulang.MasterGridViewTemplate.BestFitColumns();
}
con.Close();
}

want to check if condition whether records present in the datatable or not

SqlCommand cmd1 = new SqlCommand("select * from app where date='" + dateTimePicker2.Value + "'", connection);
// MessageBox.Show(cmd1.CommandText);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
da.Fill(dt);
//dataGridView1.DataSource = dt;
connection.Open();
if (dt.)
{
textBox1.Text = dt.Rows[0]["company"].ToString();
textBox2.Text = dt.Rows[0]["place"].ToString();
dateTimePicker1.Text = dt.Rows[0]["date"].ToString();
textBox3.Text = dt.Rows[0]["time"].ToString();
}
else
{
}
connection.Close();
control_logoff();
adminlogin();
}
In my application i want to check if data table does not have records it should not throw the error like there is no row at position 0.
How about
if(dt.Rows.Count() != 0){}
if(dt.Rows.Count > 0) should do it.

Categories

Resources