Databinding DayPilot Calendar with value from database asp.net C# - asp.net

I using DayPilot Lite 3.1 from :
http://www.daypilot.org
Now I like to fill the calendar with value from the my sql database but I don't know how ?
Can you help me, what I need to write in the protected DataTable getData()?
Here is the code behind:
namespace Rezervacii{
public partial class Events : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DayPilotCalendar1.StartDate = firstDayOfWeek(DateTime.Now, DayOfWeek.Sunday);
DayPilotCalendar1.DataSource = getData();
DataBind();
}
}
protected DataTable getData()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=.\SQLEXPRESS;Initial Catalog=Korisnik;Integrated Security=True";
SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [name], [startevent], [endevent] FROM [Event] WHERE id=#id , name=#name, startevent=#startevent, endevent=#endevent", con);
DataTable dt;
dt = new DataTable();
da.Fill(dt);
return dt;
}
private static DateTime firstDayOfWeek(DateTime day, DayOfWeek weekStarts)
{
DateTime d = day;
while (d.DayOfWeek != weekStarts)
{
d = d.AddDays(-1);
}
return d;
}
protected void DayPilotCalendar1_BeforeEventRender(object sender, DayPilot.Web.Ui.Events.Calendar.BeforeEventRenderEventArgs e)
{
string color = e.DataItem["color"] as string;
if (!String.IsNullOrEmpty(color))
{
e.DurationBarColor = color;
}
}
}
Here is the code how was before I edit :
protected DataTable getData()
{
DataTable dt;
dt = new DataTable();
dt.Columns.Add("start", typeof(DateTime));
dt.Columns.Add("end", typeof(DateTime));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("color", typeof (string));
DataRow dr;
dr = dt.NewRow();
dr["id"] = 0;
dr["start"] = Convert.ToDateTime("15:50");
dr["end"] = Convert.ToDateTime("15:55");
dr["name"] = "Event 1";
dt.Rows.Add(dr);
return dt;

You can use something like this:
protected DataTable getData()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=.\SQLEXPRESS;Initial Catalog=Korisnik;Integrated Security=True";
SqlDataAdapter da = new SqlDataAdapter("SELECT [id], [name], [startevent], [endevent] FROM [Event] WHERE NOT (([endevent] <= #start) OR ([startevent] >= #end))", con);
da.SelectCommand.Parameters.AddWithValue("start", DayPilotCalendar1.StartDate);
da.SelectCommand.Parameters.AddWithValue("end", DayPilotCalendar1.EndDate.AddDays(1));
DataTable dt;
dt = new DataTable();
da.Fill(dt);
return dt;
}

Related

System.Data.DataRowView in DropDownList instance of actual value

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["statesid"].ConnectionString);
public DataTable bindstate()
{
con.Open();
SqlCommand cmd = new SqlCommand("bindstateid",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindstateid();
}
}
public void bindstateid()
{
dal ds = new dal();
DataTable dt = new DataTable();
dt = ds.bindstate();
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
GridView1.DataSource = dt;
GridView1.DataBind();
}
I am getting system.data.datarowview in the dropdownlist instance of the actual value
You do not specify the DataTextField and DataValueField properties. These should be the names of a column in the DataTable.
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Column1";
DropDownList1.DataValueField = "Column2";
DropDownList1.DataBind();

populate gridview on dropdownlist indexchanged

i have gridview which populate date from database i want to change seclected data on dropdown SelectedIndexChanged when i select the first index it selected data from database when i chang selections it get another data try this code but nosense this is my code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
if (ddlTguidedit.SelectedIndex==0)
{
string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
else
{
string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
}
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
protected void ddlTguidedit_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlTguidedit.SelectedIndex == 0)
{
string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
else
{
string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";
SqlCommand cmd = new SqlCommand(strQuery);
GridView1.DataSource = GetData(cmd);
GridView1.DataBind();
}
}
why it doesnt work ??
Remove BindData() from !IsPostBack(). You are loading the grid data on DropDownList SelectedIndexChanged. There is no need of BindData() function to be in !IsPostBack(). BindData() function always loads no matter at what index the DropDownList is, it will always take the index as 0.

On Click Twice the Button Shows "ConnectionString property has not been initialized"

I have following code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class supervissions_pages_JwelOrder : System.Web.UI.Page
{
#region variable
ClassData clsDt = new ClassData();
SqlDataAdapter da;
DataSet ds;
#endregion
#region events
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
Session["MbrId"] = "LYM47336163";
}
protected void grdView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdView.EditIndex = -1;
BindGrid();
}
protected void grdView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Add"))
{
DropDownList item = (DropDownList)grdView.FooterRow.FindControl("ddlParticular_F");
TextBox quantity = (TextBox)grdView.FooterRow.FindControl("txtQuantity_F");
TextBox rate = (TextBox)grdView.FooterRow.FindControl("txtRate_F");
TextBox SerialCode = (TextBox)grdView.FooterRow.FindControl("txtSerial_F");
addJwelOrder(txtMbrid.Text, txtDate.Text, item.Text, quantity.Text, rate.Text, SerialCode.Text);
grdView.EditIndex = -1;
BindGrid();
}
}
protected void grdView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void grdView_RowEditing(object sender, GridViewEditEventArgs e)
{
grdView.EditIndex = e.NewEditIndex;
BindGrid();
DropDownList item = (DropDownList)grdView.FooterRow.FindControl("ddlParticular_F");
ddparticular(item);
}
protected void grdView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void txtMbrid_TextChanged(object sender, EventArgs e)
{
using (clsDt.sqlCnn)
{
clsDt.sqlCnn.Open();
SqlCommand cmd = new SqlCommand("SELECT MBR_NM FROM MBR_MST WHERE MBR_ID = #name", clsDt.sqlCnn);
cmd.Parameters.Add("#name", txtMbrid.Text);
DataTable dtt = new DataTable();
string strname = (string)cmd.ExecuteScalar();
if (!string.IsNullOrEmpty(strname))
{
txtMbrNm.Text = strname;
}
}
}
protected void txtDate_TextChanged(object sender, EventArgs e)
{
BindGrid();
}
protected void grdView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
var particular = e.Row.FindControl("ddlParticular_F") as DropDownList;
if (null != particular)
{
ddparticular(particular);
}
}
}
#endregion
#region functions
protected void BindGrid()
{
using (clsDt.sqlCnn)
{
clsDt.sqlCnn.Open();
SqlCommand cmd = new SqlCommand("USP_CRUD_JWELORDERS", clsDt.sqlCnn);
cmd.Parameters.Add(new SqlParameter("#operation", SqlDbType.VarChar, 20));
cmd.Parameters["#operation"].Value = "Display";
cmd.CommandType = CommandType.StoredProcedure;
//grdView.DataSource = cmd.ExecuteReader();
//grdView.DataBind();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
grdView.DataSource = dt;
grdView.DataBind();
}
}
protected void addJwelOrder(string strMemberID, string strDate, string strParticular, string strQuantity, string strRate, string serialCode)
{
using (clsDt.sqlCnn)
{
clsDt.sqlCnn.Open();
SqlCommand cmd = new SqlCommand("USP_CRUD_JWELORDERS", clsDt.sqlCnn);
cmd.Parameters.Add(new SqlParameter("#Operation", SqlDbType.VarChar, 10));
cmd.Parameters.Add(new SqlParameter("#memberid", SqlDbType.VarChar, 20));
cmd.Parameters.Add(new SqlParameter("#orderdt", SqlDbType.DateTime));
cmd.Parameters.Add(new SqlParameter("#item", SqlDbType.VarChar, 3));
cmd.Parameters.Add(new SqlParameter("#orderqty", SqlDbType.VarChar,20));
cmd.Parameters.Add(new SqlParameter("#rte", SqlDbType.VarChar, 8));
cmd.Parameters.Add(new SqlParameter("#serealcode", SqlDbType.VarChar, 20));
cmd.Parameters.Add(new SqlParameter("#franchise_code", SqlDbType.VarChar, 20));
cmd.Parameters.Add(new SqlParameter("#orderid", SqlDbType.VarChar, 20));
cmd.Parameters["#Operation"].Value = "Add";
cmd.Parameters["#memberid"].Value = strMemberID;
cmd.Parameters["#orderdt"].Value = strDate;
cmd.Parameters["#item"].Value = strParticular;
cmd.Parameters["#orderqty"].Value = strQuantity;
cmd.Parameters["#rte"].Value = strRate;
cmd.Parameters["#serealcode"].Value = serialCode;
cmd.Parameters["#franchise_code"].Value = Session["MbrId"].ToString();
cmd.Parameters["#orderid"].Value = clsDt.getDataTable("SELECT RIGHT(1000000+(MAX(JWL_ORID) + 1),6) AS orderid FROM JWL_ORD_MST").Rows[0][0].ToString();
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
}
}
protected void ddparticular(DropDownList ddlParticular)
{
DataTable dt = new DataTable();
ddlParticular.DataSource = clsDt.getDataTable("SELECT COM_CMCD,COM_CMNM FROM COM_MST WHERE COM_CMCD = (SELECT COM_CMCD FROM COM_TYP WHERE COM_CTNM = 'Jewellery')");
ddlParticular.DataTextField = "COM_CMNM";
ddlParticular.DataValueField = "COM_CMCD";
ddlParticular.DataBind();
}
protected void Delete(string id)
{
using (clsDt.sqlCnn)
{
clsDt.sqlCnn.Open();
SqlCommand cmd = new SqlCommand("USP_CRUD_JWELORDERS", clsDt.sqlCnn);
cmd.Parameters.Add(new SqlParameter("#Operation", SqlDbType.VarChar, 10));
cmd.Parameters.Add(new SqlParameter("#si", SqlDbType.Int));
cmd.Parameters["#Operation"].Value = "Delete";
cmd.Parameters["#si"].Value = id;
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
}
}
protected void updatemail(string strSi, string strParticular, string strQuantity, string strRate, string serialCode)
{
using (clsDt.sqlCnn)
{
clsDt.sqlCnn.Open();
SqlCommand cmd = new SqlCommand("USP_CRUD_JWELORDERS", clsDt.sqlCnn);
cmd.Parameters.Add(new SqlParameter("#Operation", SqlDbType.VarChar, 10));
cmd.Parameters.Add(new SqlParameter("#item", SqlDbType.VarChar, 3));
cmd.Parameters.Add(new SqlParameter("#orderqty", SqlDbType.Int));
cmd.Parameters.Add(new SqlParameter("#rte", SqlDbType.VarChar, 8));
cmd.Parameters.Add(new SqlParameter("#serealcode", SqlDbType.VarChar, 20));
cmd.Parameters.Add(new SqlParameter("#si", SqlDbType.Int));
cmd.Parameters["#Operation"].Value = "Update";
cmd.Parameters["#item"].Value = strParticular;
cmd.Parameters["#orderqty"].Value = strQuantity;
cmd.Parameters["#rte"].Value = strRate;
cmd.Parameters["#serealcode"].Value = serialCode;
cmd.Parameters["#si"].Value = strSi;
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
}
}
#endregion
}
and Design for it is:
The Problem with this when i click Add button for first time the data is saved sucessfully...
Second time when i click Add button it shows me error below.
classData code is:
Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Public Class ClassData
Private sqlstring As String
Private Com As SqlCommand
Private dmlString As String
Private sqlDr As SqlDataReader
Public sqlCnn As New SqlConnection(ConfigurationManager.ConnectionStrings("ConString").ToString())
Public Function getRandomVal() As String
Dim str As String
Dim rnd As New Random
str = rnd.Next(111111, 999999)
Return str
End Function
Public Function getDataTable(ByVal qry As String) As DataTable
If sqlCnn.State <> ConnectionState.Open Then sqlCnn.Open()
Dim sqlCmd As New SqlCommand(qry, sqlCnn)
Dim dt As New DataTable
' Dim sqldt As New SqlDataAdapter(qry, sqlCnn)
' Com.Connection = sqlCnn
sqlCmd.CommandTimeout = 150000
sqlDr = sqlCmd.ExecuteReader()
' sqldt.Fill(dt)
dt.Load(sqlDr)
sqlDr.Close()
If sqlCnn.State = ConnectionState.Open Then sqlCnn.Close()
Return dt
End Function
Public Function executeQuery(ByVal strQry As String) As Integer
If sqlCnn.State <> ConnectionState.Open Then sqlCnn.Open()
Dim sqlCmd As New SqlCommand(strQry, sqlCnn)
sqlCmd.CommandTimeout = 1500
' Com.Connection = sqlCnn
'Com.CommandTimeout = 15000
'sqlDr = sqlCmd.ExecuteReader()
Dim int = sqlCmd.ExecuteNonQuery()
If sqlCnn.State = ConnectionState.Open Then sqlCnn.Close()
Return int
End Function
'Public Function executeQuery(ByVal strQry As String) As DataTable
' Dim dt As New DataTable
' If sqlCnn.State <> ConnectionState.Open Then sqlCnn.Open()
' Dim sqlCmd As New SqlCommand(strQry, sqlCnn)
' dt = sqlCmd.ExecuteReder()
' If sqlCnn.State = ConnectionState.Open Then sqlCnn.Close()
' Return
'End Function
End Class

Making code duplication to refresh the gridview in asp.net

I am trying to update the gridview after updating some data. Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = University.GetConnectionString();
con.Open();
string query = "select [ID],[Name],[Surname],[level] from StudentTable order by ID";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
studentLevel.DataSource = dt;
studentLevel.DataBind();
con.Close();
}
protected void studentLevel_RowCommand(object sender, GridViewCommandEventArgs e)
{
int row = -1;
int.TryParse(e.CommandArgument as string, out row);
GridViewRow gdrow = studentLevel.Rows[row];
DataRow dr = ((DataTable)studentLevel.DataSource).Rows[gdrow.DataItemIndex];
string id = dr["ID"].ToString();
Student student = new Student(Convert.ToInt32(id), "", "", "", "", "", "", "");
if (e.CommandName == "Undergraduate")
student.setLevelRole(new UnderGraduateStudent());
else if (e.CommandName == "Graduate")
student.setLevelRole(new GraduateStudent());
student.writeLevelRole(id);
SqlConnection con = new SqlConnection(); //HERE IS DUPLICATION
//refresh the gridview on the page
con.ConnectionString = University.GetConnectionString();
con.Open();
string query = "select [ID],[Name],[Surname],[level] from StudentTable order by ID";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
studentLevel.DataSource = dt;
studentLevel.DataBind();
con.Close();
}
The problem is, if i do not write the last 12 lines of the code, which is the same code as page_load method, the gridview does not refresh itself on the page. Waht can i do to avoid this?
Thanks
You should provide a method that loads the data and databinds the GridView, for example DataBindGrid:
private void DataBindGrid()
{
using(var con = new SqlConnection(University.GetConnectionString()))
{
con.Open();
string query = "select [ID],[Name],[Surname],[level] from StudentTable order by ID";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
studentLevel.DataSource = dt;
studentLevel.DataBind();
}
}
Then you can call it from wherever you need. If you want to change something you have only one place to maintain which is less error-prone.
Note that you should databind the GridView only if(!IsPostBack) if you use ViewState(default):
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback))
DataBindGridView();
}
and in RowCommand
protected void studentLevel_RowCommand(object sender, GridViewCommandEventArgs e)
{
// ... update student ... then
DataBindGridView();
}

Can't implicitly convert type void to object

protected void Page_Load(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS; database=employee; integrated security = SSPI"))
{
SqlCommand cmd = new SqlCommand("select * from emp",con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Option");
while (sdr.Read())
{
DataRow dr = dt.NewRow();
dr["ID"] = sdr["uid"];
dr["Name"] = sdr["uname"];
dr["Option"] = addrbl();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
private object addrbl()
{
Panel p1 = new Panel();
RadioButtonList rl1 = new RadioButtonList();
rl1.Items.Add(new ListItem("yes","1"));
rl1.Items.Add(new ListItem("no","0"));
return(p1.Controls.Add(rl1));
}
Seperate the void method from the return statement.
p1.Controls.Add(r11)); has no return value and therefore cannot be used as an argument in a return statement.
Change your return statement in addrbl() to this:
p1.Controls.Add(rl1);
return p1;
Also, you should be returning the actual object type and not just an object of type object:
private Panel addrbl() {..}

Resources