使用数据表它们在网格视图中绑定数据,在数据表中已经存储了三行,但在网格视图中它们只绑定了最后一行。有什么问题
public partial class datatable:System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{
}
DataTable dt;
string sql;
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string ConStr = "Data source=localhost; database=route; Integrated Security=SSPI;";
SqlConnection con = new SqlConnection(ConStr);
con.Open();
string source = TextBox1.Text;
string des = TextBox2.Text;
string query = " select * from split";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
string trno = dr[0].ToString();
string trname = dr[1].ToString();
string source_db = dr[2].ToString();
string route_db = dr[3].ToString();
string destination_db = dr[4].ToString();
if ((source.ToString().Trim() == source_db.ToString().Trim()) && (des == destination_db))
{
sql="select * from split where train_no='"+trno+"';";
dt = getdata(sql);
GridView1.DataSource = dt;
GridView1.DataBind();
}
// Split string on spaces. This will separate all the words in a string
if (source.ToString() == source_db.ToString())
{
string[] words = route_db.Split(' ');
foreach (string word in words)
{
if (des == word.ToString())
{
sql = @"select * from split where train_no='" + trno + "';";
dt = getdata(sql);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
if (des.ToString() == destination_db.ToString())
{
string[] words = route_db.Split(' ');
foreach (string word in words)
{
if (source == word.ToString())
{
sql = @"select * from split where train_no='" + trno + "';";
dt = getdata(sql);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
string[] words1 = route_db.Split(' ');
int flag = 0;
foreach (string word in words1)
{
if (source == word.ToString() && flag == 0)
{
flag = 1;
}
if (des.ToString() == word.ToString() && flag == 1)
{
sql = @"select * from split where train_no='" + trno + "';";
dt = getdata(sql);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
con.Close();
}
catch (Exception exp)
{
err.Text = exp.ToString();
}
}
public DataTable getdata(string sql1)
{
DataTable dt1 = new DataTable();
try
{
using (SqlConnection con = new SqlConnection("Data source=localhost; database=route; Integrated Security=SSPI;"))
{
SqlDataAdapter da = new SqlDataAdapter(sql1, con);
da.Fill(dt1);
}
}
catch (Exception excep)
{
}
return dt1;
}
}