I have the following code
SqlConnection con = new SqlConnection();
con.ConnectionString=#"Data Source=RITESH-PC\SQLEXPRESS;database=master;Integrated Security=true";
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select * from Employee2",con);
SqlDataAdapter adp1 = new SqlDataAdapter("Select * from employee1", con);
DataSet dst=new DataSet();
DataTable dt= new DataTable();
DataTable dt1 = new DataTable();
Now please tell me how to show both the tables in 1 Gridview..
Try this simple code ..
DataSet dataSet = new DataSet();
using (SqlConnection connection =
new SqlConnection(connectionString))
{
connection.Open();
SqlDataAdapter adapter =
new SqlDataAdapter ("select t1.* ,t2.* from table1 t1,table2 t2 ", connection);
adapter.Fill(dataSet);
MyGridview1.DataSource=dataSet;
MyGridview1.DataBind();
}
U can also make use of Merge function to combine 2 DataTable like this:
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=RITESH-PC\SQLEXPRESS;database=master;Integrated Security=true";
con.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select * from Employee2", con);
SqlDataAdapter adp1 = new SqlDataAdapter("Select * from employee1", con);
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
adp.Fill(dt);
adp1.Fill(dt1);
//After merge u will get merge result in dt.
dt.Merge(dt1);
While merging DataTable make sure schema must match.
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'm having problems loading a stored procedure into a DataGridView. I've searched for an answer, however my code looks similar to every answer I've found. The stored procedure runs in another DataGridView I've added where I included it as a fixed datasource. I'm new to C#. Can anyone see where I am going wrong?
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection myConn = new SqlConnection("Data Source=SERVER-SQL1;Initial Catalog=OPSystem;Integrated Security=True");
myConn.Open();
SqlCommand myCmd = new SqlCommand("spCustomers", myConn);
myCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(myCmd);
da.Fill(dt);
dataGridView1.DataSource = da;
}
You can't bind to a SqlDataAdapter. You need to bind to the DataTable.
DataTable dt = new DataTable();
SqlConnection myConn = new SqlConnection("Data Source=SERVER-SQL1;Initial Catalog=OPSystem;Integrated Security=True");
myConn.Open();
SqlCommand myCmd = new SqlCommand("spCustomers", myConn);
myCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(myCmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
For context, the SqlDataAdapter:
Represents a set of data commands and a database connection that are
used to fill the DataSet and update a SQL Server database. This class
cannot be inherited.
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter(v=vs.110).aspx
private BindingSource bindingSource1 = new BindingSource();
DataTable dt = new DataTable();
SqlConnection myConn = new SqlConnection("Data Source=SERVER-SQL1;Initial Catalog=OPSystem;Integrated Security=True");
myConn.Open();
SqlCommand myCmd = new SqlCommand("spCustomers", myConn);
myCmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(myCmd);
da.Fill(dt);
bindingSource1.DataSource = dt;
dataGridView1.DataSource = bindingSource1;
Try BindingSource
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]
I am working on an asp.net/C# project and I need to know if it is possible to convert a DataTable to a gridview in C#.
string Database = "Name Of Database";
SqlConnection Con = new SqlConnection("Data Source=SqlServerName; Initial Catalog=" + Database + ";Integrated Security=True");
SqlCommand Cmd = new SqlCommand("SELECT * FROM TableName", Con);
DataTable Table = new DataTable();
SqlDataAdapter Adapter = new SqlDataAdapter(Cmd);
Adapter.Fill(Table);
Grid.DataSource = Table;
Grid.DataBind();
I have use following code to access database,
but my datagrid do not display any records.
SqlConnection conn = new SqlConnection(#"Data Source=.\SQLEXPRESS;Initial Catalog=Sanket;Integrated Security=True;Pooling=False");
DataSet ds = new DataSet();
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM std", conn);
da.Fill(ds);
myDataGrid.ItemsSource = ds.Tables[0].DefaultView;