populate gridview on dropdownlist indexchanged - asp.net

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.

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

how to display and update data in textbox of asp.net by using stored procedure?

i want to update user information .so first i want when user click on update button than all the field display with their values in text box so that user can able to edit his profile.
i got an error :Procedure or function 'updatefill' expects parameter '#email', which was not supplied.
please help me out from this error...
Thank you.
public partial class updateuser : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString);
SqlCommand cmd;
SqlDataAdapter da;
DataTable dt;
SqlDataReader dr;
string str1="";
protected void Page_Load(object sender, EventArgs e)
{
{
con.Open();
cmd = new SqlCommand("updatefill", con);
cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"registr");
fname.Text = ds.Tables["registr"].Rows[0]["fname"].ToString();
lname.Text = ds.Tables["registr"].Rows[0]["lname"].ToString();
uid.Text = ds.Tables["registr"].Rows[0]["email"].ToString();
pwd.Text = ds.Tables["registr"].Rows[0]["password"].ToString();
pwd1.Text = ds.Tables["registr"].Rows[0]["confirmpassword"].ToString();
mbl.Text = ds.Tables["registr"].Rows[0]["mobile"].ToString();
con.Close();
}
}
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
str1 = "Male";
}
else if (RadioButton2.Checked)
{
str1 = "Female";
}
else
{
str1 = "please select gender";
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("update_user_details", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#fname", SqlDbType.VarChar).Value = fname.Text;
cmd.Parameters.Add("#lname", SqlDbType.VarChar).Value = lname.Text;
cmd.Parameters.Add("#email", SqlDbType.VarChar).Value = uid.Text;
cmd.Parameters.Add("#mobile", SqlDbType.VarChar).Value = mbl.Text;
cmd.Parameters.Add("#password", SqlDbType.VarChar).Value = pwd.Text;
cmd.Parameters.Add("#confirmpassword", SqlDbType.VarChar).Value = pwd1.Text;
cmd.Parameters.Add("#gender", str1);
dt = new DataTable();
da = new SqlDataAdapter(cmd);
da.Fill(dt);
int s = Convert.ToInt32(dt.Rows[0][0]);
if (s == 1)
{
errmsg.Text = "Update Successfull";
Response.Redirect("userhome.aspx");
}
else
{
errmsg.Text = "Update Faild";
con.Close();
}
ALTER PROCEDURE [dbo].[updatefill]
#email varchar(50)
AS
BEGIN
select * from registr where email=#email
END
Here is an example using the Reader:
String connStr = ConfigurationManager.ConnectionStrings["DB1"].ConnectionString;
String cmdStr = "SELECT [col2],[col3] FROM [Table1] WHERE [col1]=#col1;";
try
{
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand(("updatefill", con);
{
cmd.CommandType = C0mmandType.StoredProcedure;
conn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
fname.Text = rdr[0];
lname.Text = rdr[1]
uid.Text = rdr[2]
pwd.Text = rdr[3]
pwd1.Text = rdr[4];
mbl.Text = rdr[5];
}
}
conn.Close();
cmd.Dispose();
conn.Dispose();
}
}
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}

lable is not showing any output for the dropdown list selection

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LibraryConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlbookavail.Items.Add(new ListItem("select", "-1"));
ddlbookavail.AppendDataBoundItems = true;
SqlCommand cmd = new SqlCommand("select * from tblbookinfo", con);
con.Open();
ddlbookavail.DataSource = cmd.ExecuteReader();
ddlbookavail.DataTextField = "Name";
ddlbookavail.DataValueField = "Id";
ddlbookavail.DataBind();
con.Close();
txtdate.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
}
in my design page i put the sql datasource from data and got this connection srting

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

dynamic ActiveTabChanged for AJax Tab container

How to fire ActiveTabChanged for TabContainer. I have written a code to populate the TabPanel HeaderText from the database and this is my code
public partial class dynamicTab : System.Web.UI.Page
{
string strCon = ConfigurationManager.ConnectionStrings["SqlCon"].ConnectionString.ToString();
TabContainer ajxTab;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// createTab();
pnlDynamic.Controls.Add(ajxTab);
ajxTab_ActiveTabChanged(ajxTab, EventArgs.Empty);
}
}
private void ajxTab_ActiveTabChanged(TabContainer ajxTab, EventArgs eventArgs)
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select * from Employee where DeptID='" + ajxTab.ActiveTab.ID.ToString() + "'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView grd = new GridView();
grd.AutoGenerateColumns = true;
grd.DataSource = ds;
grd.DataBind();
pnlDynamic.Controls.Add(grd);
}
protected void page_init(object sender, eventargs e)
{
createtab();
}
private void createTab()
{
SqlConnection con = new SqlConnection(strCon);
SqlCommand cmd = new SqlCommand("select DeptID,DepartmentName from Department", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
ajxTab = new AjaxControlToolkit.TabContainer();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TabPanel pnl = new TabPanel();
pnl.HeaderText = ds.Tables[0].Rows[i]["DepartmentName"].ToString();
pnl.ID = ds.Tables[0].Rows[i]["DeptID"].ToString();
ajxTab.Tabs.Add(pnl);
ajxTab.ActiveTabIndex = 0;
}
}
}
For the first time ActiveTabChanged fired perfectly , but when I click on the second tab ActiveTabChanged is not getting fired. I tried by setting ajxTab.AutoPostBack=true but the tab container is not getting visible when the event occurs.
This is my sample output
On clicking computers I would like to load grid with those details so can some one help me
You can bind the content of each tab in the OnActiveTabChanged event
OnActiveTabChanged="TabContainer1_ActiveTabChanged"
AutoPostBack="true"

Resources