How to bind data to label in asp.net - c#

I have created a form in asp.net,where i have some textbox, in which I have taken some input which is stored in the database. I have created another page where I have taken the same number of label as the textbox.
I want to show the data in the labels that I last entered.
How can i do that?
Code:
string cs = ConfigurationManager.ConnectionStrings["TrishanConnection"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 1 coil_id FROM CoilDetails ORDER BY coil_id DESC", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
LabeCoilid.Text = ds.ToString();
LabeCoilid.DataBind();
con.Close();

Try instead of
LabeCoilid.Text = ds.ToString();
this
LabeCoilid.Text = ds.Tables[0].Rows[0][0].ToString();

LabeCoilid.Text = ds.Tables[0].Rows[0]["coil_id"].ToString();
You don't need to call DataBind()

string cs = ConfigurationManager.ConnectionStrings["TrishanConnection"].ConnectionString;
using(SqlConnection con = new SqlConnection(cs))
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 1 coil_id FROM CoilDetails ORDER BY coil_id DESC", con))
{
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(ds);
dt=ds.Table[0]
}
}
LabeCoilid.Text = dt.Rows[0][0].ToString();
LabeCoilid.DataBind();
con.Close();
You can change the rows and columns by changing dt,Rows[4][8]

Related

How display table from combobox

Hi i have combobox with Table names. I would like to display selected table on button click. How to do it? This is code of my combobox
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT name FROM sys.tables", Con);
SqlDataReader rdr;
rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Load(rdr);
wczytywanie.ValueMember = "name";
wczytywanie.DataSource = dt;
Con.Close();
now i would like to display these tables in dataGridView after button click.This is not working.
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from sys.tables where name=#name", Con);
cmd.Parameters.Add("name", wczytywanie.SelectedValue.ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Dane.DataSource = dt;
Con.Close();
try adding DisplayMember and assign value "name"
wczytywanie.ValueMember = "name";
wczytywanie.DisplayMember = "name";
This might do
conn = new MySqlConnection(connectString);
conn.Open();
comm = new MySqlCommand(query, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
DataSet DS = new DataSet();
adapter.Fill(DS);
advancedDataGridView1.DataSource = DS.Tables[0];
conn.Close();
And to change the column name your sql be like columnname as 'your preferred column name'
SELECT principal_id as 'Principal ID' from sys.tables where name=#name"

C# Parameterized query - parameters not being replaced with set value

I'm passing a query and parameter from a WinForm to a database class. The
The code on the Form looks like this:
string selectedComp = "CPSI";
string catsQuery = "SELECT id, category, old_value, old_desc, new_value, new_desc, reference1, reference2 FROM masterfiles.xref WHERE company_name = '#company' ORDER BY category, old_value";
Db categoriesData = new Db();
dgvCategories.DataSource = categoriesData.GetData(catsQuery, selectedComp);
And in my database class my code to populate the datatable/set is this:
public DataTable GetData(string selectQuery, string selectedComp)
{
NpgsqlConnection conn = new NpgsqlConnection(connString);
DataSet ds = new DataSet();
NpgsqlCommand cmd = new NpgsqlCommand(selectQuery, conn);
cmd.Parameters.Add(new NpgsqlParameter("#company", selectedComp));
//cmd.Parameters.AddWithValue("#company", selectedComp);
//cmd.Parameters.Add("#company", NpgsqlDbType.Text);
//cmd.Parameters["#company"].Value = selectedComp;
try
{
conn.Open();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);
conn.Close();
da.Fill(ds);
return ds.Tables[0];
}
}
But putting a breakpoint at NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);, selecctQuery hasn't changed - the '#company' is still in the query.
What am I missing?
The root problem is that you're passing the query to the data adapter instead of the command. Change
NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectQuery, conn);
to
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
I would also use using to dispose of all objects, and don't close the connection until the dataset is filled:
using(NpgsqlConnection conn = new NpgsqlConnection(connString))
using(NpgsqlCommand cmd = new NpgsqlCommand(selectQuery, conn))
{
cmd.Parameters.Add(new NpgsqlParameter("company", selectedComp));
conn.Open();
using(NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
}
conn.Close();
return ds.Tables[0];
}

Assign query result into a session

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"];

How add Select as first value to a database populated combobox in asp windows forms

SqlConnection con = getConnection();
SqlDataAdapter da = new SqlDataAdapter(storedprocedureName, con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
con.Open();
DataSet DS = new DataSet();
da.Fill(DS);
stateDropDownList.DataSource = DS.Tables[0];
stateDropDownList.ValueMember = "STATE_ID";
stateDropDownList.DisplayMember = "STATE_NAME";
This data source is returning state details and i have attached the satedetails datasource into combobox.
i want to display the combobox with first item select and rest as all states.
i googled it a lot for dropdown list there are codes but nothing showing ...
It can be done in the same way as Drop Down list:
ComboBox1.Items.Insert(0, "--Select--");
Entire code:
SqlConnection con = getConnection();
SqlDataAdapter da = new SqlDataAdapter(storedprocedureName, con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
con.Open();
DataSet DS = new DataSet();
da.Fill(DS);
ComboBox1.DataSource = DS.Tables[0];
ComboBox1.ValueMember = "STATE_ID";
ComboBox1.DisplayMember = "STATE_NAME";
ComboBox1.Items.Insert(0, "--Select--");

Want to show data from two different tables in two different datagridviews in a form in c#

I have two different queries for two different tables I want to show the result in two datagridviews on a form
string query1 = string.Format("select * from Flat where [Flat_No.]='{0}'",flat.Text);
string query2 = string.Format("select * from 1");
SqlCommand cmd = new SqlCommand(query1, con);
SqlCommand cmd1 = new SqlCommand(query2, con1);
dataview frm1 = new dataview(query1,query2); //the form where data is to be displayed
// on form dataview I have two DataGridViews
public dataview(string a,string b)
{
InitializeComponent();
SqlConnection con = new SqlConnection(Class1.getConnectionString);
//connection name
con.Open();
SqlCommand cmd = new SqlCommand(a , con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
dataGridView1.DataSource = ds.Tables["ss"];
con.Close();
SqlConnection con1 = new SqlConnection(Class1.getConnectionString);
//connection name for query1
con1.Open();
SqlCommand cmd1 = new SqlCommand(b, con1);
cmd1.CommandType = CommandType.Text;
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
DataSet ds1 = new DataSet();
da.Fill(ds1, "aa");
dataGridView2.DataSource = ds1.Tables["aa"];
con1.Close();
}
}
but the above code is showing data from query 1 in both the datagridviews. plz help me out how can I solve this problem? If there in another way let me know it also. I have also tried to merge both the queries using "+" sign but it also didn't proved helpful.
Use da1.Fill instead of da.fill. You're using the da DataAdapter for filling both Datasets
da.Fill(ds1, "aa");
da1.Fill(ds1, "aa");

Categories

Resources