I have a simple search page in ASP.NET page where I am filling dataset based on SQL Query.
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
da.Fill(ds, "Emp");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
else
{
Label2.Text = "Data not found";
}
con.Close();
But even the search item exists, I get this result as "Data Not found".. Why isn't it executing if statement?
As the others suggested you need to move a line of code like so:
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "Emp"); // SEE THIS LINE!
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
Label2.Text = string.Empty;
}
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
Label2.Text = "Data not found";
}
con.Close();
You are missing
dataadapter.fill(ds)
It is likely not working because you already executed the query prior to trying to fill the dataset. Try removing cmd.ExecuteNonQuery();. Also, a Non Query command doesn't return any results. Make sure that your query actually does return results.
hi please Try this its Working for my gridview
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("Select * from Tbl_Employee", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
gv1.DataSource = ds.Tables[0];
gv1.DataBind();
}
}
Related
i want assign result into a session
c# code
protected void BindData()
{
SqlConnection con = new SqlConnection(#"Data Source=DESKTOP-677TN4G\SQLEXPRESS;Initial Catalog=homework;Persist Security Info=True;User ID=sa;Password=123456");
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
con.Open();
string cmdstr = "Select CourseName from Staff where FacultyNumber=#idd";
SqlCommand cmd = new SqlCommand(cmdstr, con);
cmd.Parameters.AddWithValue("#idd", Session["id"].ToString());
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}
so how i can do it i try to do
Session["id"] = cmdstr.Text;
but it not work
i do it like this but not work
I do it like this
protected void BindData()
{
SqlConnection con = new SqlConnection(#"Data Source=DESKTOP-677TN4G\SQLEXPRESS;Initial Catalog=homework;Persist Security Info=True;User ID=sa;Password=123456");
DataSet ds = new DataSet();
DataTable FromTable = new DataTable();
con.Open();
string cmdstr = "Select CourseName from Staff where FacultyNumber=#idd";
SqlCommand cmd = new SqlCommand(cmdstr, con);
cmd.Parameters.AddWithValue("#idd", Session["id"].ToString());
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
DataList1.DataSource = ds.Tables[0];
Session.Add("Staff", ds.Tables[0]);
DataList1.DataBind();
Label1.Visible = true;
Label1.Text = "Course Name is : " + Session["Staff"].ToString();
}
and the output is
Course Name is : Table
not the selected value
In your case the result is a DataTable no problem you can store this DataTable to the session as well. Let staffData be the DataTable that you are populating from the database,
Session.Add("Staff", staffData); // Adding datatable to the session
In your case you are using ds is used as a DataSet and you are accessing ds.Tables[0] to bind the grid. in this case you can use something like: Session.Add("Staff", ds.Tables[0]);.
Later you may come up with another question that how can I retrieve this DataTable from the session, Here I'm adding the answer for that as well.
DataList1.DataSource = (DataTable)Session["Staff"];
I want to show content of a table("newexample") in my database in SQL server to gridview in ASP.net, but the gridview shows nothing. How to fix it?
Here is the code:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string cs = ConfigurationManager.ConnectionStrings["SampleDBCS"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from newexample", con);
DataTable dt = new DataTable();
dt.Columns.Add("col0");
dt.Columns.Add("col1");
dt.Columns.Add("col2");
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
DataRow dr = dt.NewRow();
dr["col0"] = rdr["col0"];
dr["col1"] = rdr["col1"];
dr["col2"] = rdr["col2"];
dt.Rows.Add(dr);
}
con.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
Did you get any exception? Try moving con.Close() after DataBind() and see how it goes.
Try to use the code below:
DataSet dataSet = new DataSet("newexample");
SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from newexample", connectionString);
dataAdapter.Fill(dataSet);
if (dataSet != null)
{
if (dataSet.Tables[0].Rows.Count != 0)
{
GridView1.DataSource = dataSet ;
GridView1.DataBind();
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
}
}
This is a better approach since SqlDataAdapter will take care for SqlConnection. It is responsible to create the connection and to close it at the end of operation in the best way possible.
This is not a tested code so if you have any problem let me know..
well, I've done everything on my behalf just to solve this problem. i'm trying to print the data from my database using grid view in asp.net using c# codes. can anyone tell me whats wrong and how to improve my codes. thank you.
using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["DBCon"].ConnectionString))
{
constructor var = new constructor();
con.Open();
string sql = "SELECT first_name,last_name,username,contact_number,address,email FROM user_tbl WHERE user_type='2'";
MySqlCommand cmd = new MySqlCommand(sql, con);
MySqlDataReader reader1 = cmd.ExecuteReader();
reader1.Close();
try
{
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
lblresult.Text = "ERROR>>" + ex.Message + "!";
}
finally
{
con.Close();
sql = null;
}
You must fill the DataSet with data like this :
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "TableName");
GridView1.DataSource = ds.Tables["TableName"];
GridView1.DataBind();
You're assigning an empty DataSet to your DataSource, without filling the results of your DataReader into the DataSet/DataTable.
using (MySqlConnection con = new MySqlConnection(""))
{
con.Open();
string sql = "SELECT first_name,last_name,username,contact_number,address,email FROM user_tbl WHERE user_type='2'";
MySqlCommand cmd = new MySqlCommand(sql, con);
try
{
DataTable dt = new DataTable();
using (MySqlDataReader reader1 = cmd.ExecuteReader())
{
dt.Load(reader1);
}
GridView1.DataSource = dt ;
GridView1.DataBind();
}
catch (Exception ex)
{
lblresult.Text = "ERROR>>" + ex.Message + "!";
}
finally
{
con.Close();
sql = null;
}
}
Guys i want to print two records returned from a database to a label. I use dataset , browse through the column however could't bind as label does not have a datasource. Below is my code.
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
lblClient.Enabled = true;
lblClient.Text = Convert.ToString(ds.Tables[0].Columns[0]);
lblBranch.Text = Convert.ToString(ds.Tables[0].Columns["Bname"]);
}
connection.Close();
when i tried the above. It returns only the column name (i.e the string specified). Any alternative would be appreciated.
You don't want the column's text but the DataRow values:
lblClient.Text = ds.Tables[0].Rows[0].Field<string>(0);
lblBranch.Text = ds.Tables[0].Rows[0].Field<string>(1);
or by column-name (presumed the name of the first):
lblClient.Text = ds.Tables[0].Rows[0].Field<string>("Cname");
lblBranch.Text = ds.Tables[0].Rows[0].Field<string>("Bname");
This works only if the table contains at least one row. So you have to check that.
you need to indicate the row has you want. With your code you select a column of DataTable
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
lblClient.Enabled = true;
lblClient.Text = Convert.ToString(ds.Tables[0].Rows[0].[0]);
lblBranch.Text = Convert.ToString(ds.Tables[0].Rows[0].["Bname"]);
}
connection.Close();
I'm trying to store un saved records based on query in dataset to show it in grid view to the user with no luck, How can I save them in dataset using the following code ?
for (int i = 1; i < recs.Tables[0].Rows.Count; i++)
{
try
{
validateQuery = "SELECT .... "
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(validateQuery, conn);
DataSet ds = new DataSet();
da.Fill(ds, "candRecs");
DataTable dt = new DataTable();
dt = ds.Tables[0];
int RowsCount = dt.Rows.Count;
if (RowsCount == 0)
{
//conn.Open();
cmd = new SqlCommand(query, conn);
cmd.Parameters.AddWithValue("#CAND_CARD_NO", recs.Tables[0].Rows[i][0].ToString());
....
cmd.ExecuteNonQuery();
}
else
{
x++;
//What I should to write here to store the records in Dataset ??
}
...