I want to retrive data from database(mysql) into array string and then want to retrive more data from database based on that array string values. plz help
here is my code
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Collections;
public partial class searchresult : System.Web.UI.Page
{
public int i;
public string comment, search,wd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindParentDataList();
}
}
public void BindParentDataList()
{
ArrayList list = new ArrayList();
SqlCommand cmd1 = new SqlCommand("SELECT * from searches", con);
cmd1.Connection = con;
if (con.State == ConnectionState.Closed)
con.Open();
SqlDataReader rd = cmd1.ExecuteReader();
rd.Read();
list.Add(rd["strings"] + "");
rd.Close();
foreach (string s in list)
{
SqlCommand cmd = new SqlCommand("select * from comment WHERE (CommentOn LIKE \'%" + (s + "%\')"), con);
SqlDataReader myDataReader = cmd.ExecuteReader();
parentDataList.DataSource = myDataReader;
parentDataList.DataBind();
myDataReader.Close();
}
if (con.State == ConnectionState.Open)
con.Close();
i retrive data from table searches and then use the data of searches table to retrive data from comment table. help please
Loading
amna naveedPosted Aug 16, 2014, 3:14 AM
DataTable dataTable = new DataTable();
SqlCommand cmd1 = new SqlCommand("SELECT * from searches", con);
con.Open();
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd1);
// this will query your database and return the result to your datatable
da.Fill(dataTable);
da.Dispose();
foreach (DataRow row in dataTable.Rows)
{
comments = row["strings"] as string;
SqlCommand cmd = new SqlCommand("select * from comment WHERE (CommentOn LIKE \'%" + (comments + "%\')"), con);
SqlDataReader myDataReader = cmd.ExecuteReader();
parentDataList.DataSource = myDataReader;
parentDataList.DataBind();
myDataReader.Close();
}
only the data that is in the text box is displayed but i want to retrive all the data from database that is in the string comments. sir plz help
R JPosted Aug 15, 2014, 11:19 PM
I would optimize your code in BindParentDataList method this way, so that it will be easy. Hope this helps. Let me know if you have questions,
DataTable dataTable = new DataTable();
string connString = @"your connection string here";
string query = "select * from table";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
// this will query your database and return the result to your datatable
da.Fill(dataTable);
conn.Close();
da.Dispose();
foreach (DataRow row in dataTable.Rows)
{
string comments = row["ColumnName_you_have"] as string;
/////similar way get the datatable and bind it in your way.
}