In my project,
I check the condition and stored the data from database to data table and bind that to grid view..
problem is in grid view they bind the last row only , previous row are overwrite , What is the problem
HELP ME
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Fathima tasleemPosted Oct 18, 2013, 1:51 PM
{
}
DataTable dt;
string sql;
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string ConStr = "Data source=localhost; database=trainroute; 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();
// search via source and destination
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
// search via source and destination(route)
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();
}
}
}
//search via source(route ) and destination
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();
}
}
}
// Search via source and destination in routes
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=Busroute; Integrated Security=SSPI;"))
{
SqlDataAdapter da = new SqlDataAdapter(sql1, con);
da.Fill(dt1);
}
}
catch (Exception ex)
{
err2.Text = ex.ToString();
}
return dt1;
}
}
Db Structure:
tr_no, tr_name, source, route, destination
this is my code , I'm new asp.net(c#), so help me sir.. Thank You
selva kumarPosted Oct 17, 2013, 3:43 AM
here is sample..
DataTable Dt = new DataTable();
Con.Open();
Sdap = new SqlDataAdapter("Select Query", Con);
Sdap.Fill(Dt);
Con.Close();
foreach (DataRow row in Dt.Rows)
{
for (int i = 0; i <= Dt.Rows.Count; i++)
{
string assignname= Dt.Rows[i]["columnname"].ToString();
.
.
.
<.....Condition check...>
Gridview binding goes here...
}
}
For more details u should upload ur source code..then only i can solve ur problem...
LaxmikantPosted Oct 16, 2013, 3:54 PM