为什么数据表没有用SqldataAdapter填充? [英] Why doesn't the data table filling up with SqldataAdapter?

查看:62
本文介绍了为什么数据表没有用SqldataAdapter填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这段代码有什么问题?数据表没有填满....为什么?

Hi,

What''s the issue with this piece of code? The data table is not filling up.... why?

SqlConnection con = new SqlConnection(Application["con"].ToString());
        con.Open();
        SqlDataAdapter ad = new SqlDataAdapter("SELECT  *   FROM    Students", con);
        DataTable dtbl = new DataTable();
        ad.Fill(dtbl);
        gdvStudents.DataSource = dtbl;
        gdvStudents.DataBind();
        con.Close();


在此先感谢


Thanks in advance

推荐答案

您的代码似乎是正确的.无法告诉您为什么不逐行调试它

在这里.尝试这个.对我有用

Your code seems to be correct. Cannot tell you why without debugging it line by line

Here.. Try this. It works for me

//Connection string to the server
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = "Password=sasasasas;Persist Security Info=True;User ID=sa;Initial Catalog=sasasas;Data Source=sasasas;";
        //This doe sthe task of fetching data from sql server
        SqlDataAdapter adp = new SqlDataAdapter();
        //Create the command object
        SqlCommand command=new SqlCommand();
        command.CommandText="SELECT * FROM Students";
        //Inline query or stored procedure
        command.CommandType = CommandType.Text;
        command.Connection = connection;
        adp.SelectCommand = command;
        //Open the connedction to sql server
        connection.Open();

        //Fill to datatable
        DataTable dt=new DataTable();
        adp.Fill(dt);
        connection.Close();

        GridView1.DataSource = dt;
        GridView1.DataBind();


connection.ConnectionString = "Password=sasasasas;Persist Security Info=True;User ID=sa;Initial Catalog=sasasas;Data Source=sasasas;";

也许您无法连接SQL

maybe you can,t connecting SQL


问题出在%#DataBinder.Eval(Container.DataItem,"Student_Name")%>

我错过了代码中的".DataItem"(%#DataBinder.Eval(Container,"Student_Name")%>)
现在可以正常使用了...

谢谢大家的帮助....
Problem was with %#DataBinder.Eval(Container.DataItem, "Student_Name")%>

I missed ".DataItem" in my code (%#DataBinder.Eval(Container, "Student_Name")%> )
Now it works fine ...

Thanks all of you for the help....


这篇关于为什么数据表没有用SqldataAdapter填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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