Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi every body
i have gridview which populate date from database i want to change seclected data on dropdown SelectedIndexChanged i try this code but nosense this is my code

C#
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 ??
Posted
Updated 27-Jul-14 0:59am
Comments
Kornfeld Eliyahu Peter 27-Jul-14 7:14am    
Please explain what "doesn't work" means...
ost3z 27-Jul-14 9:55am    
when i select from drpdownlist the first choise or next chosie the grid still have the same data without change ? it should change on select changed

Your query inside both if and else block seems to be getting the data from Table Tbl_uploadpdf. You also don't have any condition for retrieving the data. It is directly getting all the data from the Table.
 
Share this answer
 
Comments
ost3z 27-Jul-14 12:07pm    
my condition is if (ddlTguidedit.SelectedIndex==0) if it the selected from dropdown list itis going to carry out the querry else its going to carry out the other chosie but no sense what should do ? to work fine
You did not get me. Look at the query you are executing inside if and else...

Inside if block
---------
string strQuery = "SELECT [Pdfid],[Arpdf_name],[Arpdf_des],[pdf_date] FROM [books_alaa].[dbo].[Tbl_uploadpdf]";

Inside else Block
-----------------
string strQuery = " SELECT Pdfid, Enpdf_name AS Arpdf_name, Enpdf_des AS Arpdf_des, pdf_url, pdf_date FROM Tbl_uploadpdf";

Which are basically the same. So, your GridView is showing the same data. I don't know your business logic, otherwise I could have told you how to make the query.

I guess you need to add one WHERE condition to the Query in else Block in order to get some different data.
Add a where condition in your query for your else part otherwise there is no difference in the fetched data for if-else condition.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900