Click here to Skip to main content
15,036,654 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to write the code for following situation:
[select the item from dropdownlist then after clicking on button i have to show the information of only selected item in gridview]
. plz help me.
I have created the code but by using my code whole information are seen in gridview.
C#
 SqlConnection cn = new SqlConnection();
            cn.ConnectionString = @"Data Source='XYZ-88EEDEE15AC\SQLEXPRESS';Integrated Security=True;Pooling=False;Initial Catalog='legal'";
            cn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            string str = DropDownList1.SelectedItem.Text;
            cmd.CommandText = "select * from branch where" + str;
            cmd.ExecuteNonQuery();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
adp.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write("Error is" + ex.Message);

        }
Posted
Updated 8-Jan-13 16:51pm
v2

There is a problem in following line

C#
cmd.CommandText = "select * from branch where" + str;


It will be like this

C#
cmd.CommandText = "select * from branch where columnname='" + str +"'";


Note: Use parameterized query for better performance.


Thanks
   
Comments
amu jamdade 8-Jan-13 23:09pm
   
your answer gives following error:
Error isIncorrect syntax near 'item name '.
[no name] 8-Jan-13 23:44pm
   
'item name' means, that's the column name or dropdown selected item name
Hi,

Please be use proper select Statement.

SQL
select * from table_name where column_name='string'
or
select * from table_name where column_name=int


please be use proper syntax in select statement
   
Hello,
Here is some issue in your codes,

command text will like this

cmd.CommandText = "select * from Teble_name where columnname = '" + str +"'";


No need this line because ExecuteNonQuery is use for INSERT/UPDATE/DELETE Operation no need for Select operation
cmd.ExecuteNonQuery();



here is a complete code for select data may this help you

string ConnectionString = ConfigurationManager.ConnectionStrings["ConStrName"].ConnectionString;
            DataTable dt = new DataTable();
            string sql = "SELECT * FROM TABLE_NAME WHERE COLUMNNAME = '" + value + "';
            //Take the connection 
            SqlConnection connection = new SqlConnection(ConnectionString);
            try
            {
                //now open the connection
                connection.Open();
                //Take a command with query and connection
                SqlCommand command = new SqlCommand(sql, connection);
                //say the command type
                command.CommandType = CommandType.Text;                
                //take a dataadapter for selecting data 
                SqlDataAdapter DataAd = new SqlDataAdapter(command);
                //fill the datatab 
                DataAd.Fill(dt);
                //close the connection 
                connection.Close();
            GridView1.DataSource = dt;
            GridView1.DataBind();
                //return the table 
                return dt;
            }
            catch (Exception ex)
            {
                connection.Close();
                throw new Exception(ex.Message.ToString());
 
                //or you can throw exception 
            }
   
Comments
amu jamdade 9-Jan-13 1:34am
   
thanks for solution
Ronjon1 9-Jan-13 1:36am
   
Please Mark as Solved if it help... :)
You have several issues. There is no space after 'where'. str needs to contain all the sql ( such as myNAme = 'fred' ). Finally, building SQL by string mashing leaves you open to the most basic of attacks, and should never be done. And of course, you should not be calling SQL from your presentation layer
   

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