A way to convert a DataTable to a gridview? - c#

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();

Related

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

Fill DataTable from Oracle Database Table - C#

I have successfully built connection string and able to populate table data when the database is Access as:
DataTable results = new DataTable();
using (OleDbConnection thisConnection = new OleDbConnection(connectionname))
{
OleDbCommand cmd = new OleDbCommand("SELECT * from TABLE_A", thisConnection); //EDIT : change table name for Oracle
thisConnection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(results);
}
I am new to Oracle though. Can somebody mention what changes to make in above code for Oracle database?
You can try this;
OracleConnection conn = new OracleConnection("Your Connection string");
//Open the connection to the database
conn.Open();
DataSet dataSet = new DataSet();
OracleCommand cmd = new OracleCommand("your select query");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
using (OracleDataAdapter dataAdapter = new OracleDataAdapter())
{
dataAdapter.SelectCommand = cmd;
dataAdapter.Fill(dataSet);
}

Loading DataGridView from Stored Procedure

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

Filling the grid View with more than 1 table?

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.

how to access all table field in datagrid of WPF in ADO.net

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;

Categories

Resources