FormatException was unhandled by user code.
The string was not recognized as a valid DateTime.
protected void Page_Load(object sender, EventArgs e)
{
// to simulate a database query
socialEvents = new DataTable();
socialEvents.Columns.Add(new DataColumn("Date", typeof(DateTime)));
socialEvents.Columns.Add(new DataColumn("Description", typeof(string)));
socialEvents.Columns.Add(new DataColumn("Url", typeof(string)));
DataRow row;
row = socialEvents.NewRow();
row["Date"] = DateTime.Now.AddDays(-5);
row["Description"] = "Work";
row["Url"] = "http://www.url.cz";
socialEvents.Rows.Add(row);
}
Error in the following methode:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
**DataRow[] rows = socialEvents.Select(
String.Format(
"Date >= #{0}# AND Date < #{1}#",
e.Day.Date.ToShortDateString(),
e.Day.Date.AddDays(1).ToShortDateString()**
)
);
I suggest to use LINQ to do this:
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
var rows = socialEvents.Rows.Cast<DataRow>
.Where(r => (DateTime)r["Date"] >= e.Day.Date
&& (DateTime)r["Date"] <= e.Day.Date.AddDays(1))
.ToArray();
);
There is no more hassle with string formatted queries: you can use a filter based on real values.
Of course, if Date can be null, you will have to handle this. Just tell and I will edit this code if needed.
Related
i want to bind my data Gridview but it throw an error.
i allow paging when i click on new page e.g 1.2.3 then the above error throw
here is my code.
the one is page index change and the other one is my method of BindGrid.
the error come when i click one new page 2 or 3 etc
updated code
protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataSource = Session["data"] as DataTable;
DataGrid1.DataBind();
}
private void BindGrid()
{
DataTable data = storedProcedureManager.sp_inactiveFiles(
providerID,
days, CaseTypeID, CollectorID, user.UserRegID);
lblMsg.Text = data.Rows.Count + " Record's Found.";
log.Info(lblMsg.Text);
Session["data"] = data;
DataGrid1.DataSource = data;
DataGrid1.DataBind();
log.Info("Report Displayed.");
}
Problem what is happening is when you fetch the data again in BindGrid() method you must be getting less number of pages.so you can do two things.
1)If you are filtering your data then reset the page index to 1.
protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
BindGrid();
}
private void BindGrid()
{
DataTable data = storedProcedureManager.sp_inactiveFiles(
providerID,
days,CaseTypeID,CollectorID,user.UserRegID);
lblMsg.Text = data.Rows.Count + " Record's Found.";
log.Info(lblMsg.Text);
DataGrid1.DataSource = data;
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataBind();
log.Info("Report Displayed.");
}
2)If you are not filtering your data then store the datagrid value in a session object and use this for pageing.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataSource = Session["value"] as DataTable;
DataGrid1.DataBind();
}
private void BindGrid()
{
DataTable data = storedProcedureManager.sp_inactiveFiles(
providerID,
days,CaseTypeID,CollectorID,user.UserRegID);
lblMsg.Text = data.Rows.Count + " Record's Found.";
log.Info(lblMsg.Text);
Session["value"]=data;
DataGrid1.DataSource = data;
DataGrid1.DataBind();
log.Info("Report Displayed.");
}
DataGrid1.DataSource = data;
DataGrid1.DataBind();
DataGrid1.CurrentPageIndex = 0;
Reset the CurrentPageIndex to 0 after the page has been bound.
I have created a simple db named Hospital and I have a few columns .I have filled first dropdown named drdoctors.And it works
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
fetchDoctors();
}
}
void fetchDoctors() {
HospitalEntities entitiy = new HospitalEntities();
List<Doctor> doc = entitiy.Doctors.ToList();
drDoctor.DataSource = doc;
drDoctor.DataTextField = "Name";
drDoctor.DataValueField = "DoctorNo";
drDoctor.DataBind();
}
What I want to do is fill the other dropdown with the this doctor's patients .
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id= Int32.Parse( drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
var query= from p in entities.Doctors
}
But linq queries are so complicated.How can i do this
This should about do it. Please note, this code wasn't tested and may contain minor errors.
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id= Int32.Parse( drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
var query= (from d in entities.Doctors
join m in entities.MedExams on d.DoctorNo equals p.DoctorNo
join p in entities.Patients on m.PatientNo equals p.PatientNo
where d.DoctorNo == id
select p).ToList();
//Populate Patients from query
}
Looking at your diagram, it looks you don't have foreign key relationships set up. I would highly recommend doing this (for uncountable reasons). But by doing this, you will be able to "join" on tables much more easily like this:
protected void drDoctor_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Int32.Parse(drDoctor.SelectedValue);
HospitalEntities entities = new HospitalEntities();
drDoctor.DataSource = entities.Doctors
.Where(x => x.DoctorNo == id)
.SelectMany(x => s.MedExams.Select(y => y.Patients));
drDoctor.DataBind();
}
Not sure why but i cant get my data to return. It starts out saying there is not data records to display. But when i select the month and year of the information i am looking for the grid view disappears. Any suggestions
NutritionEntities context;
protected void Page_Load(object sender, EventArgs e)
{
context = new NutritionEntities();
if (!IsPostBack)
{
for (int i = DateTime.Now.Year; i > 1988; i--)
{
ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
}
protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedMonth = int.Parse(ddlMonth.SelectedValue);
int selectedYear = int.Parse(ddlYear.SelectedValue);
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
GridView1.DataBind();
}
you have verify the binding of your control grid, i suggest add only object to the datasource of the grid on the pageLoad when the postback its false like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource =
from exercise in context.Exercises
where exercise.exerciseDate.Value.Month == selectedMonth && exercise.exerciseDate.Value.Year == selectedYear
select new
{
exercise.exerciseDate,
exercise.duration,
exercise.caloriesBurned,
exercise.averageHR,
exercise.distanceInMiles,
exercise.notes
};
}
GridView1.DataBind();
}
the problem is when you add the objects to datasource , when any event refresh the web page like select the control is unbinding, i hope help you,, see you!!
Is this possible?
I have a table with user accounts retrieved from a database. Then at the start of each column I would like to add a checkbox which if selected will select the account. I tried experimenting with it and I can't seem to put the checkbox inside the table. How can I do this?
You can try this:
const int ColumnSelect = 0;
protected void Page_Load(object sender, EventArgs e)
{
//Get real data here.
DataTable dt = new DataTable();
dt.Columns.Add("count");
dt.Rows.Add(dt.NewRow());
dt.Rows[0][0] = "5";
GridView1.Columns.Add(new TemplateField());
BoundField b = new BoundField();
GridView1.Columns.Add(b);
b.DataField = "count";
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
e.Row.Cells[ColumnSelect].Controls.Add(new CheckBox());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach(GridViewRow row in GridView1.Rows)
{
//Could also use (CheckBox)row.Cells[ColumnSelect].FindControl if you give the checkboxes IDs when generating them.
CheckBox cb = (CheckBox)row.Cells[ColumnSelect].Controls[0];
if (cb.Checked)
{
//Do something here.
}
}
}
I am doing something wrong in this small code
Page1: `
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
UserType = DDlUserType.SelectedItem.Text;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the value in the hyperlink column.
string HyperLinkValue = e.Row.Cells[1].Text;
HyperLink myLink = new HyperLink();
myLink.NavigateUrl = "~/ShowMMBProfileStats1.aspx?Profile_ID={0}";
myLink.Text = HyperLinkValue;
}
In ShowMMBProfileStats1.aspx
protected void Page_Load(object sender, EventArgs e)
{
int MMBProfileID = Convert.ToInt32(Request.QueryString[0]);
}
It gives me an error
Input string was not in a correct
format.
In the aspx page, I am assigning datakeynames="Profile_ID"
How do I carry this Profile_ID to page1.
`
Thanks
Sun
Try the following:
myLink.NavigateUrl =
"~/ShowMMBProfileStats1.aspx?Profile_ID=" + e.Row.Cells[profileIDCellIndex].Text;
You're not setting the value for {0}. Try something like this:
myLink.NavigateUrl =
String.Format("~/ShowMMBProfileStats1.aspx?Profile_ID={0}", HyperLinkValue );
or
myLink.NavigateUrl = "~/ShowMMBProfileStats1.aspx?Profile_ID=" + HyperLinkValue ;