Gridview from button click inside a popup window - asp.net

I have a text box with event name and two calendar controls to get the date and a button to display a gridview.
I need to display the grid with the details like eventname start date and end date from the database..
My code is .aspx.cs
protected void lnbtnSearch_OnClick(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=CTSINDLFVMOSS;Initial Catalog=DB_CGTPO_DEVE;Persist Security Info=True;User ID=*****;Password=*****");
SqlDataAdapter adapter = new SqlDataAdapter("select EventId,EventName,StartDate,EndDate,Tactics,Perct_VolLift,Perct_ROI from TableNamewhere (( EventName='" + textfield3 + "') and (StartDate= StartDate) and ( EndDate= EndDate))", conn);
DataSet ds = new DataSet();
adapter.Fill(ds);
grdEventDetails.DataSource = ds;
grdEventDetails.
}

set grdEventDetails.AutoGenerateColumns = true;
add grdEventDetails.DataBind();
but it will look ugly (especially date fields) if you don't define the columns manually.

Related

How to insert dropdownlist value, text box value and gridviews selected row together?

How to insert dropdownlist value, text box value and gridviews selected row together?
Also attached the screenshot of the data insert:
enter image description here
enter image description here
enter image description here
I tend to prefer a add button that adds a row to the grid, and THEN you let the user edit the one row.
However, assuming you have those text boxes at the top of the screen, enter data, and then click a button to add to the database, and then re-fresh the GV?
I also don't see some button in the top area to add the data?
so, say a button added to the top area?
then this would add the row to the database:
protected void Button3_Click(object sender, EventArgs e)
{
string strSQL =
"INSERT INTO MyTable (Session, Class, Section, Term, Subject, HighestMark) " +
"VALUES (#Session, #Class, #Seciton, #Term, #Subject, #HighestMark)";
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL,conn))
{
conn.Open();
cmdSQL.Parameters.Add("#Session", SqlDbType.Int).Value = DropSession.selecteditem.value;
cmdSQL.Parameters.Add("#Class", SqlDbType.Int).Value = DropClass.selecteditem.value;
cmdSQL.Parameters.Add("#Section", SqlDbType.Int).Value = DropSection.selecteditem.value;
cmdSQL.Parameters.Add("#Term", SqlDbType.Int).Value = DropTerm.selecteditem.value;
cmdSQL.Parameters.Add("#Subject", SqlDbType.Int).Value = DropSubject.selecteditem.value;
cmdSQL.Parameters.Add("#HighestMark", SqlDbType.Int).Value = HighestMark.Text;
cmdSQL.ExecuteNonQuery();
}
}
ReloadGrid(); // call routine to refresh the grid.
}
Edit: Adding the rows to a new table
Ok, so we are to take the top values, and then process each row of the grid, and take the ones with a check box and "insert" this data into a new table.
Ok, so the code will look like this:
protected void Button1_Click(object sender, EventArgs e)
{
string strSQL = "SELECT * FROM tblHotelsA WHERE ID = 0";
DataTable rstData = MyRst(strSQL);
foreach (GridViewRow gRow in GHotels.Rows)
{
CheckBox ckChecked = gRow.FindControl("ckSelected") as CheckBox;
if (ckChecked.Checked)
{
// this row was checked - add a new row
DataRow MyNewRow = rstData.NewRow();
MyNewRow["HotelName"] = txtHOtelName.Text; // exmaple control
MyNewRow["City"] = txtCity.Text; // example control above grid
// values from grid row
// tempalted columns, we use find control
MyNewRow["FirstName"] = (gRow.FindControl("txtFirstName") as TextBox).Text;
MyNewRow["LastName"] = (gRow.FindControl("txtLastName") as TextBox).Text;
// if data bound column, then we use cells collection
MyNewRow["FavorateFood"] = gRow.Cells[5];
// etc. etc.
rstData.Rows.Add(MyNewRow);
}
}
// done adding to table, write/save back to database.
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
SqlDataAdapter da = new SqlDataAdapter(cmdSQL);
SqlCommandBuilder daU = new SqlCommandBuilder(da);
cmdSQL.Connection.Open();
da.Update(rstData);
}
}
}
I also had this helper routine - I often use it all over the palce:
public DataTable MyRst(string strSQL)
{
DataTable rstData = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
cmdSQL.Connection.Open();
rstData.Load(cmdSQL.ExecuteReader());
}
}
return rstData;
}
So, the concept here?
I pulled a blank row (SELECT * from table where ID = 0).
This gets me the database structure - and thus avoids a BOATLOAD of parameters in the sql.
this approach ALSO allows me to add a row to the table, and add "many" rows. And then execute ONE update to the database to write out (save) all the rows in one shot. So, this again reduced quite a bit of messy code.

Asp.Net Datagrid view is not updating on new insertion

Well As the title explains it all so here is the code i used so far
public partial class data : System.Web.UI.Page
{
System.Data.SqlClient.SqlConnection con3;
System.Data.SqlClient.SqlDataAdapter da;
DataSet ds1;
DataTable dt;
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
con3 = new System.Data.SqlClient.SqlConnection();
con3.ConnectionString="Data Source=localhost; initial catalog=test;user id=xx;password=xxxx;";
con3.Open();
ds1 = new DataSet();
SqlCommand cmd3 = new SqlCommand();
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.CommandText = "selectdata";
cmd3.Connection = con3;
da = new SqlDataAdapter (cmd3);
da.Fill(ds1, "abc");
con3.Close();
dt = new DataTable();
dt = ds1.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
Stored Procedure is
Create PROCEDURE [dbo].[selectdata]
AS
BEGIN
select * from data
END
i searched stack overflow & did search on Google but nothing happened.
i tried closing connection after data bind, defining a new data set & creating the table again & much more please help
Try reloading the page after insertion.
The Answer Above just refresh the page so in my view it should be
1--> Define A Function & bind data in this such as
private void bind_data()
{
ASPxGridLookup2.DataSource = dt_employee;
ASPxGridLookup2.DataBind();
ASPxGridLookup3.DataSource = dt_attendance;
ASPxGridLookup3.DataBind();
ASPxGridView1.DataSource = dt_attendance;
ASPxGridView1.DataBind();
}
2--> Call this function whenever you need to refresh the datagrid such as right after insertion
obj2.insertion();
bind_data();

Why is not this gridview loading the results?

I am trying to fill the gridview with the search results. Here is the UI of my "AddDropClasses" page:
When this page is loaded, i want the gridview to be filled the current courses of the current user, and i do this using the Register table. Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = Userfunctions.GetConnectionString();
con.Open();
string query = "select * from RegisterTable where StudentID='" + MyGlobals.currentID + "'";
SqlDataAdapter adap = new SqlDataAdapter(query, con);
DataTable tab = new DataTable();
adap.Fill(tab);
showCourses.DataSource = tab;
}
I load (come to) this page by clicking a button from another page. The problem is, the table shows no results. When i debugged i realized that Myglobals.ID has the value what i already expect. What can be the problem here? Can anyone help?;
Thanks
You forgot to databind showCourses. Add showCourses.DataBind(); after showCourses.DataSource = tab;

sorting filtered data in asp.net listview

I've created a listview that's filled up with a list of guitars from the database on page load like so:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["GuitarsLTDBConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM Guitars", con);
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);
DataTable dt = new DataTable();
da.Fill(dt);
lvGuitars.DataSource = dt;
lvGuitars.DataBind();
}
The following code filters that list of guitars by a certain Make when the user checks the checkbox corresponding to that make
protected void chkOrgs_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = (DataTable)lvGuitars.DataSource;
DataView dv = new DataView(dt);
if (chkOrgs.SelectedValue == "Gibson")
{
dv.RowFilter = "Make = 'Gibson' OR Make='Fender'";
}
lvGuitars.DataSource = dv.ToTable();
lvGuitars.DataBind();
}
Now, what I want to do is be able to sort the latest data that is present within the listview. Meaning, if sort is clicked before filtering, the it should sort all data. If sort is clicked after filtering, it should sort the filtered data. I'm using the following
code, which is triggered upon a LinkButton click
protected void lnkSortResults_Click(object sender, EventArgs e)
{
DataTable dt = (DataTable)lvGuitars.DataSource;
DataView dv = new DataView(dt);
dv.Sort = "Make ASC";
lvGuitars.DataSource = dv.ToTable();
lvGuitars.DataBind();
}
The problem is that all the data that the listview was loaded with before any filtering is sorted, and not the latest filtered data. How can I change this code so that the latest data available in the listview is the one that's sorted?
Thanks
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["GuitarsLTDBConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT * FROM Guitars", con);
SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, con);
DataTable dt = new DataTable();
da.Fill(dt);
lvGuitars.DataSource = dt;
lvGuitars.DataBind();
ViewState["dt"] = dt;
}
The loading need to be set like this. Access/Update the Viewstate as well on filter and Sorting.

Finding the item in selecteditemchanging event

In listview im showing product information .On each row there is one select button on each row in itemTemplate .
if user click this button i want which bookid is clicked in selectedindexchanging event.
i bound listview like following
string str = "SELECT BookName,BookPrice, Description, bookid FROM productinfo Where Categoryid ='" + Request.QueryString["CategoryId"] + "'";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
lstvwShopItems.DataSource = dt;
lstvwShopItems.DataBind();
Please give solution?
in order to get a certain column when selecting a row you need to fill the property datakeynames with the property of your choice.
msdn
getting the value of the property:
void lstvwShopItems_SelectedIndexChanged(Object sender, EventArgs e)
{
string value = lstvwShopItems.SelectedValue;
}
don't forget to set the OnSelectedIndexChanged on your listview to this method in the codebehind..

Resources