asp.net(c#)GridView绑定的一些问题sql语句

来源:百度知道 编辑:UC知道 时间:2024/05/22 16:02:39
先大概讲下我的系统。一个页面登陆用的,登陆进去后的页面有一个GridView。
GridView那个页面的代码大概是:
public partial class XinxiCS_XinxiCS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bind();
}

this.TextBox1.Text = Convert.ToString(Session["username"]);

//我用这句话把登陆时输入的账号传给 TextBox1.Text ,登陆的账号是123,那么TextBox1.Text就是123
}

public void bind()
{

SqlConnection strcon = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conStr1"]);
strcon.Open();

string strsql = "select * from T_Xinxi where username = TextBox1.Text;
//这句代码是错的,但是我想要的效果就是这样, where username = TextBox1.Text里面的内容。TextBox1.Text是123的话,GridView绑定的就 where username = 123的内容。TextBox1.Text是456的话,GridView绑定的就 where username = 456的内容。
请路过的各位好心人帮小弟写下正确的代码,我是菜鸟,不怎么会表达,

string strsql = "select * from T_Xinxi where username ='"+ TextBox1.Text+"'";

呵呵,这个简单,你把那句改成:
string strsql = "select * from T_Xinxi where username = '"+TextBox1.Text+"';
这样就ok了,呵呵...

如果 username 是字符
string strsql = "select * from T_Xinxi where username = '"+TextBox1.Text+"'";

如果 username 是数字
string strsql = "select * from T_Xinxi where username = "+Convert.ToInt32(TextBox1.Text);

一楼正解

楼上您们都是错的!

代码顺序1:
if (!IsPostBack)
{
this.bind();
}
代码顺序2:
this.TextBox1.Text = Convert.ToString(Session["username"]);

他先调用this.bind(),此时this.TextBox1.Text 的值为空。
应该改成这样:

if (!IsPostBack)
{
this.TextBox1.Text = Convert.ToString(Session["username"]);
this.bind();
}

然后将