System.Data.DataRowView in DropDownList instance of actual value - asp.net

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

Related

asp bootstrap-select not working

asp .net 4.0 Use bootstrap-select When running the code behind, the dropdown list is not displayed. But If not running the code behind, the dropdown list is displayed.
page.aspx
<asp:DropDownList ID="ddl" runat="server" CssClass="selectpicker" OnSelectedIndexChange="ddlChange" AutoPostBack="true">
</asp:DropDownList>
page.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
ddl.Style["display"] = "inline";
SqlConnection sqlCon = new SqlConnection(ConnectString);
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("select name from customer", sqlCon);
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddl.DataSource = dt;
ddl.DataValueField = "name";
ddl.DataTextField = "name";
ddl.DataBind();
sqlCon.Close();
}
How do I fix it?
Thank you.
Ok. Seeing from your comment you could use below:
if(dt.Rows.Count>0)
{
ddl.Visible= true;
}
else
{
ddl.Visible= false;
}
protected void Page_Load(object sender, EventArgs e) {
ddl.Style["display"] = "inline";
SqlConnection sqlCon = new SqlConnection(ConnectString);
sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand("select name from customer", sqlCon);
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataTable dt = new DataTable();
da.Fill(dt);
ddl.DataSource = dt;
ddl.DataValueField = "name";
ddl.DataTextField = "name";
ddl.DataBind();
sqlCon.Close();
}

ASP.NET C#,Javascript

Want to populate the second dropdown values based on selection first of first drop down.
id Bankname Branchname
1 HDFC AMT
2 HDFC gag
3 icic AMsfsaT
4 bpjn daD
in the first drop down if I select the HDFC then in second dropdown AMT,gag should come.
I am using following code to fill first dropdown.
string com = "Select * from mst_bank";
SqlDataAdapter adpt = new SqlDataAdapter(com, con);
DataTable dt = new DataTable();
adpt.Fill(dt);
ddlbank.DataSource = dt;
ddlbank.DataBind();
ddlbank.DataTextField = "Bankname";
ddlbank.DataValueField = "ID";
Add 2 DropDownLists:
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" /><br />
<asp:DropDownList ID="ddl2" runat="server" />
And your code behind will be:
private string conStr = WebConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDDL1();
FillDDL2(ddl1.SelectedValue);
}
}
protected void FillDDL1()
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("Select * from mst_bank", con);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
ddl1.DataSource = ds;
ddl1.DataTextField = "Bankname";
ddl1.DataValueField = "ID";
ddl1.DataBind();
}
catch (Exception ex)
{
//work with exception
}
finally
{
con.Close();
}
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
FillDDL2(ddl1.SelectedValue);
}
protected void FillDDL2(string id)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand("Select * from second_table where BANK_ID = #id", con);
cmd.Parameters.Add("#id", SqlDbType.Int).Value = id;
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
ddl1.DataSource = ds;
ddl1.DataTextField = "Name";
ddl1.DataValueField = "ID";
ddl1.DataBind();
}
catch (Exception ex)
{
//work with exception
}
finally
{
con.Close();
}
}
On PostBack we fill dropdown1 and dropdown2, using corresponding value from the dropdown1. Method ddl1_SelectedIndexChanged will be occur when user changes selection, and dropdown2 will be filled with a corresponding values. Of course, you have to change a SQL-query there.

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.

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

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

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

Resources