在网格视图中显示数据 [英] show the data in grid view

查看:82
本文介绍了在网格视图中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写以下情况的代码:

[从下拉列表中选择项目然后点击按钮后我必须在gridview中显示所选项目的信息]

。 PLZ帮助我。

我创建了代码,但是使用我的代码可以在gridview中看到整个信息。

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.

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

        }

推荐答案

以下行存在问题



There is a problem in following line

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





这将是这样的





It will be like this

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





注意:使用参数化查询以获得更好的性能。





谢谢



Note: Use parameterized query for better performance.


Thanks


您好,



请使用正确的选择声明。



Hi,

Please be use proper select Statement.

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





请在select语句中使用正确的语法



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 +"''";





不需要此行因为ExecuteNonQuery用于INSERT / UPDATE / DELETE操作不需要选择操作



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 
            }


这篇关于在网格视图中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆