ADO.NET: Data not being displayed properly in repeater - c#

I am facing a peculiar issue in my ADO.NET code. This is the table data that I am accessing from a repeater from the frontend.
1 get car cleaned 2012-02-14 08:32:25.643 NULL
2 submit tax documents 2012-02-14 08:33:04.610 NULL
3 photo copy all documents 2012-02-14 08:33:04.610 NULL
Data in the first row is not being displayed at all.
If I delete the rows 2 and 3, no data is being displayed in the repeater. I think the issue is with my ADO.NET code. Also, if I truncate the table completely, the page is loading forever as opposed to displaying the "No Data Found" message in the label.
protected void Page_Load(object sender, EventArgs e)
{
txtNewTask.Focus();
if (!IsPostBack)
{
GetTaskList();
}
}
protected void GetTaskList()
{
conn = new SqlConnection(cstr);
getTasksCmd = new SqlCommand("select Name, CreationDate, CompletionDate from tasks", conn);
try
{
using (conn)
{
conn.Open();
using (reader = getTasksCmd.ExecuteReader())
{
while (!reader.Read())
{
lblDbMsg.Text = "No Data Found!";
}
rptTaskList.DataSource = reader;
rptTaskList.DataBind();
}
}
}
catch (Exception)
{
throw;
}
}

Take out the while loop from your code:
using (reader = getTasksCmd.ExecuteReader())
{
rptTaskList.DataSource = reader;
rptTaskList.DataBind();
}
Because you are calling SqlDataReader.Read() once, you are moving past the first record. Therefore, if you want to be able to retrieve all of the rows of data including the first, don't call Read() at all.
SqlDataReader.Read() Method MSDN Reference

Related

NullReferenceException after displaying a table on DataGridView more than once

Edit 2: Turns out it only gives a NullReferenceException the first time. After that, it says that I am not allowed to change the ConnectionString property, even though I think I have closed it in the right places.
Thanks for taking the time to read this.
I am working on a WinForms application that uses an MS Access database, and I am currently having problems with an entry deletion feature I made.
So, I have a DataGridView that switches between 3 tables on a button click, and I have a function that deletes a row on a table that is currently open by clicking a button that is at the end of the row.
When I open my first table, and try to delete a row, it works just fine. However, if I open a different table afterwards and try to delete an entry, or even go back to the first table I opened, I get a NullReferenceException in the deletion function.
Here is the code to display one of the tables in DataGridView.
public DataTable Read()
{
connection.ConnectionString = connectionString;
OpenConnection(); //connection.Open() inside an if statement
dataTable.Clear();
OleDbCommand readStudentCommand = new OleDbCommand("select * from Students", connection); //display the whole list of students
OleDbDataReader reader = readStudentCommand.ExecuteReader();
dataTable.Load(reader);
connection.Close();
return dataTable;
}
Here is the code that deletes an entry
private void MainDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
connection.ConnectionString = connectionString;
if (ConnectionState.Closed == connection.State)
{
connection.Open();
}
var senderGrid = (DataGridView)sender;
if (e.RowIndex >= 0 && senderGrid.Columns[e.ColumnIndex] == MainDataGridView.Columns["Delete"])
{
//this function retrieves the first column value of the deleted row, which has the ID of the entry (works with any table).
DeleteEntry(MainDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()); //exception thrown here (System.Windows.Forms.DataGridViewCell.Value.get returned null)
MainDataGridView.Rows.RemoveAt(e.RowIndex);
}
and here is DeleteEntry()
private void DeleteEntry(string deletedID)
{
string tableName = null;
string idType = null;
if (studentsDisplayed)
{
tableName = "Students";
idType = "Student ID";
}
else if(booksDisplayed)
{
tableName = "Books";
idType = "BookID";
}
else if(loansDisplayed)
{
tableName = "Loans";
idType = "Loan ID";
}
string deleteCommand = String.Format("DELETE * FROM {0} WHERE [{1}] = {2}", tableName, idType, deletedID);
OleDbCommand deleteEntryCommand = new OleDbCommand(deleteCommand, connection);
deleteEntryCommand.ExecuteNonQuery();
SaveData(); //this method just calls Update() in a dataAdapter of a relevant table
connection.Close();
}
Thank you!
Edit:
As per request, here is the code that switches the table. It simply references the first function and sets the returned dataTable as DataSource.
private void StudentButton_Click(object sender, EventArgs e) //display students
{
try
{
if (!studentsDisplayed)
{
MainDataGridView.DataSource = studentDAL.Read(); //studentDAL is the class that works with the Students table of my DB.
studentsDisplayed = true; //these 3 are to avoid duplicated creation of the same table
booksDisplayed = false;
loansDisplayed = false;
ComboBoxChanger(); //don't mind this, it's for an irrelevant feature
CreateButtons(5);
}
}
catch
{
throw;
}
}
Okay, so turns out the problem was the fact that DeleteEntry(MainDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString()) had a problem with the Cells[0] part. After the first time loading a table, the 0th cell just vanished. So, I rewrote the code so that instead of declaring tableName and idType in DeleteEntry(), they're declared in MainDataGridView_CellContentClick(), and then made the DeleteEntry() accept 3 idType and tableName as parameters, and changed the MainDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString() argument to MainDataGridView.Rows[e.RowIndex].Cells[idType].Value.ToString(). Now it works just fine!

How to display employeephoto from database on listbox item click

I have a listbox that retrieves employeedata, and when I click on employee, I get his firstname, lastname and his photo.
I did everything until displaying the image from database. I have this code below. When I click on employeename in listbox, I want to load and display his photo alongside with his name too.
(SQL column EmployeePhoto is "image" type, where I successfully inserted employee images as binary.
Crawled this subject in stack but did not find a useful solution that I could understand. Need your kind help, thanks.
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) //select listbox item
{
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT EmployeeFirstName,EmployeeLastName,EmployeePhoto FROM Employees WHERE EmployeeID = #myvalue", cnn);
cmd.Parameters.AddWithValue("#myvalue", (ListBox1.SelectedValue));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
TextBox1.Text = dr.GetString(0);
TextBox2.Text = dr.GetString(1);
//want to display EmployeePhoto in ID:Image1 that was saved as binary
}
}
cnn.Close();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
I would save the image to the file System (using File.WriteAllBytes()). And then show the file from the file system in the image box.
You need image handler (ASHX) in order to display binary data to browser.
Image Handling In ASP.NET
For example,
using System;
using System.Web;
public class ImageHandler : IHttpHandler, IReadOnlySessionState
{
public void ProcessRequest(HttpContext context)
{
// Query image data from database based on query string value.
context.Response.BinaryWrite(YourImageByte);
}
public bool IsReusable {
get { return false; }
}
}
// Usage
Image1.ImageUrl ="ImageHandler.ashx?id=5"
You should use ListView control and not ListBox.
ListViewiItem has an ImageIndex property,that enables you to specify the index of the image to render from the it (It supports binding as well)

corrupt database & listbox problems

im working in WinForms C#.
for some reason when I want to populate my listBox it stops and says my database is corrupt.
I have added a repair line and the codes run afterwards, but nothing happends. My listbox is not populated.
Here is the code im using.:
public void button1_Click(object sender, EventArgs e)
{
SqlCeConnection cn = new SqlCeConnection(#"Data Source = Database1.mdf");
cn.Open();
SqlCeCommand cm = new SqlCeCommand("SELECT * FROM tblprojects ORDER BY Projekt_liste ASC", cn);
try
{
SqlCeDataReader dr = cm.ExecuteReader();
while (dr.Read())
{
ListBox project_list = Application.OpenForms["Form1"].Controls["tabControl1"].Controls["tabPage1"].Controls["Project_list"] as ListBox;
project_list.Items.Add(dr["Projekt_liste"].ToString());
}
cn.Close();
cn.Dispose();
}
catch (Exception ex)
{
}
}
public void button2_Click(object sender, EventArgs e)
{
SqlCeConnection cn = new SqlCeConnection();
SqlCeEngine engine = new SqlCeEngine("Data Source = Database1.mdf");
if (false == engine.Verify())
{
MessageBox.Show("Database is corrupted.");
engine.Repair(null, RepairOption.RecoverAllPossibleRows);
}
}
For example if you want to load the items
1. make sure you have a ListBox on the winform
2. name the ListBox
3. Create a ListItem
4 Add the ListItem to the ListBox
while(dr.Read())
{
ListViewItem obj=new ListViewItem(Convert.ToString(dr[0]),Convert.ToString(dr[1]);
//in object of ListViewItem give display member at first and give value member at second position
listView1.Items.Add(obj); // add object to the listbox
}
Here are a few links that you can use as well to show different ways on how to populate a ListBox
one is Windows and the other will be if you are using or plan to use ASP.NET
Populate a ListBox when using SQLDataReader
asp.net SqlDataReader example: how to use Read() method to populate ListBox
Populate ASP.NET ListBox using SqlDataReader
From Microsoft Site
The Repair method does not guarantee complete data recovery for every
database. Some forms of data corruptions cannot be repaired
completely, regardless of the Repair option that is selected by the
application.
This could be one of the case where your file is corrupted.
Also please try To have repair call straight above the call to populate Data in List.
This may help.

Transfer data to another table and make previous GridView empty

I have GridView. When I click a button, it sends data to another table and makes the previous GridView empty.
I have the following code for the button click, but it does not make the previous GridView empty.
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string userApply = "insert into Company (CompanyName) select (JobTitle) from Jobs where JobTitle=JobTitle";
SqlCommand insertApply = new SqlCommand(userApply, conn);
try
{
insertApply.ExecuteNonQuery();
conn.Close();
Response.Redirect("ApplyJob.aspx");
}
catch (Exception er)
{
Response.Write(er.ToString());
}
finally
{
}
}
}
}
Are you clearing the previous gridview anywhere?
Maybe try this before your redirect:
grvPrevious.DataSource = null;
grvPrevious.DataBind();
it looks like you have your GridView in ApplyJob.aspx, since you are redirecting to that page in your try block and there you see the gridview holding some values. You may pass a query string along with ApplyJob.aspx and then in your form load of ApplyJob.aspx check for that query string. If you find the value then clear the Gridview. Something on the following line..
In your try block do :
Response.Redirect("ApplyJob.aspx?ClearGridView=YES");
In your Form_Load event of the ApplyJob.aspx check for the query string
if(Request.QueryString["ClearGridView"] != null && Request.QueryString["ClearGridView"] =="YES")
{
yourGridView.DataSource = null;
youGridView.DataBind();
}

DataGrid.Items filled with Null data

I'm trying to fill a asp.DataGrid with data from a DB2 database. The problem I'm getting is that the data is coming back null. The weird thing is, the records themselves (seem to) load from the database, as when I step through the code while debugging, the DataGrid.Items.Count = the number of records I have in the database itself.
Additionally, while troubleshooting, I've added an asp.Label that's initially hidden to display the number of records found in the DataGrid itself, and each time it displays the correct number of records.
Here's my code:
protected void Page_Load(object sender, EventArgs e)
{
dta_grd = new DataGrid();
dta_grd = Ex_DLL.GetData("select * from tstint/m02");
Lbl_Dsply.Visible = true;
if (Supp_Data.Items.Count == 0)
{
Lbl_Dsply.Text = "No Records Found!";
}
else
{
Lbl_Dsply.Text += dta_grd.Items.Count.ToString();
}
}
Ex_DLL is simply the name of a library that does all the connections to the Database itself.
The Code for Ex_DLL.GetData():
public static DataGrid GetData(string str_sql)
{
//EX_Global.DB2_Conn("DEV") is just an internal connection function that houses
//the connection settings.
iDB2Connection db2_conn = EX_Global.DB2_Conn("DEV");
iDB2Command db2_cmd = null;
iDB2DataAdapter db2_adpt = null;
DataSet dta_ds = new DataSet();
DataGrid ret_val = new DataGrid();
try
{
if (db2_conn.State != System.Data.ConnectionState.Open) { db2_conn.Open(); }
db2_cmd = new iDB2Command(str_sql, db2_conn);
db2_adpt = new iDB2DataAdapter(db2_cmd);
db2_adpt.Fill(dta_ds);
ret_val.DataSource = dta_ds;
ret_val.DataBind();
db2_conn.Close();
}
catch (Exception) { }
return ret_val;
}
Now, when I read them individually using idb2DataReader, it's actually reading from the database, but there's just something lost in translation from reading the database to filling the DataGrid itself.
Any ideas?
There are two problems (may be more) I can see:
You instantiate the DataGrid control during page execution but forget to add it to page's Controls collection.
You missed Datasource property and DataBind() method.
In design environment, add a "PlaceHolder" control on a page (.aspx) (Say PlaceHolder1)
protected void Page_Load(object sender, EventArgs e)
{
dta_grd = new DataGrid();
dta_grd.DataSource = Ex_DLL.GetData("select * from tstint/m02");
dta_grd.DataBind(); // this method populates the DataGrid from assigned datasource
PlaceHolder1.Controls.Add(dta_grd);
Lbl_Dsply.Visible = true;
if (Supp_Data.Items.Count == 0)
{
Lbl_Dsply.Text = "No Records Found!";
}
else
{
Lbl_Dsply.Text += dta_grd.Items.Count.ToString();
}
}

Categories

Resources