I want to display records in pivot grid (devexpress). The data will return from Stored Procedure
pivotTest is the name of pivotgrid,I can give the connection String later. But I don't how to bind data to pivot grid.
One more doubt. In which scenario,we should use SqlDataReader and SqlDataAdapter
private void ReportTestForm_Load(object sender, EventArgs e)
{
string cs = "";
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spProcedureTest", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
pivotTest.DataSource = rd;
pivotTest.DataBindings;
}
}
Related
I want to display my oracle sql table on datagridview using windows form on visual studio 2017 c#. I am using oracle.data.access.client and type
private void button1_Click(object sender, EventArgs e)
{
string oradb = "Data Source=127.0.0.1;User Id=practice;Password=practice;";
OracleConnection conn = new OracleConnection(oradb);
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from test_dummy";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
dataGridView1.DataSource = dr;
conn.Dispose();
}
I don't get it.. How do i make this work? . >.<
DataSource of DataGridView in this case should be DataTable not DataReader so I think you should change your code like this
OracleDataReader dr = cmd.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(dr);
dataGridView1.DataSource = dataTable;
If you change like this and debug it has data but still nothing to display, you should check your data column binding
when i load my application i need to fill the combobox cbkeuze with the row loginnaam from table gebruik
The error I am getting is: Can't change the items because property Data Source is set.
Here is my code:
private void Form1_Load(object sender, EventArgs e)
{
// SQL Connectie opzetten
SqlConnection Conn = new SqlConnection();
Conn.ConnectionString = #"Integrated Security=true;Initial Catalog=Wachtwoord;Data Source=LAPTOP-PDI9B3LP\SCHOOL";
Conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = Conn;
// Alles selecteren van tabel Favorieten
cmd.CommandText = "select * from gebruik";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
// Tabel wegschrijven in Applicatie
string loginnaam = dr.GetString(0);
cbKeuze.Items.Add(loginnaam);
}
dr.Close();
// Database connectie sluiten
Conn.Close();
}
You should check your combobox properties and see if DataSource is initiated
You can add loginnaam's to List loginnaams until while loop ends. After dr.Close() line, use cbKeuze.DataSource = loginnaams.
This will work for you.
Why does my grid view not refresh with data in it? It adds to the database but then clears my grid view and has nothing in it. It loads the form and I can enter information into it hit the add button and it adds to database but grid view refresh puts no data into it.
private void addButton_Click(object sender, EventArgs e)
{
string title = titleTextBox.Text;
string starring = starringTextBox.Text;
string year = yearTextBox.Text;
SqlConnection conn = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=<PATH TO FILE>);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO dbo.Movie (Title, Starring, Year) VALUES (#title, #starring, #year)";
cmd.Parameters.AddWithValue("#Title", title);
cmd.Parameters.AddWithValue("#Starring", starring);
cmd.Parameters.AddWithValue("#Year", year);
cmd.Connection = conn;
cmd.ExecuteNonQuery();
SqlDataAdapter MyDataAdapter = new SqlDataAdapter(cmd.CommandText, conn);
titleTextBox.Text = "";
starringTextBox.Text = "";
yearTextBox.Text = "";
titleTextBox.Focus();
movieTableAdapter.Fill(moviesDataSet.Movie);
movieBindingSource.DataSource = movieTableAdapter;
myDataGridView.DataSource = movieBindingSource;
myDataGridView.Refresh();
myDataGridView.Update();
conn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'moviesDataSet.Movie' table. You can move, or remove it, as needed.
this.movieTableAdapter.Fill(this.moviesDataSet.Movie);
}
SqlDataAdapter MyDataAdapter = new SqlDataAdapter("select query here", conn);
Pass select query to adapter. You are passing an insert query.
I have two tables in a SQL Server database. I select from table ADMS and I need to insert master table by gridview but I dont know how to insert with gridview. Please help. I've tried for many days and I did not pass yet
protected void Button3_Click1(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
SqlConnection con = new SqlConnection(MyConnectionString);
// con.Open(); // don't need the Open, the Fill will open and close the connection automatically
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM ADMS_Machining where datetime='" + TextBox1.Text + "'", con);
mytable = new DataTable();
da.Fill(mytable);
GridView2.DataSource = mytable;
GridView2.DataBind();
}
else
{
SqlConnection con = new SqlConnection(MyConnectionString);
// con.Open(); // don't need the Open, the Fill will open and close the connection automatically
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Machining_Master where datetime='" + TextBox1.Text + "'", con);
mytable = new DataTable();
da.Fill(mytable);
GridView2.DataSource = mytable;
GridView2.DataBind();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
String strConnString, strSQL;
strConnString = "Server=kane-pc;UID=sa;PASSWORD=1234;Database=Machining;Max Pool Size=400;Connect Timeout=600;";
//here
conn.ConnectionString = conn;
conn.Open();
cmd.Connection = conn;
cmd.CommandText = strSQL;
}
You can extract values from a grid view depending on what you have placed in the cells...
string value = this.GridView2.Rows[0].Cells[0].Text;
You can also track the selected row event, and get specific controls like the following...
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
string someValueTakenFromLabel = (GridView2.SelectedRow.FindControl("lblAnyLabelHere") as Label).Text;
// .... do something with value here
}
I suggest you go through some tutorials though to get the hang of how to use GridView.
http://www.asp.net/web-forms/videos/building-20-applications/lesson-8-working-with-the-gridview-and-formview
http://www.aspsnippets.com/Articles/How-to-get-Selected-Row-cell-value-from-GridView-in-ASPNet.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview%28v=vs.110%29.aspx
You have to first read data from cells and then insert them into database using SqlCommand.
Assuming that you have M_ID and M_NAME columns in your Machining_Master table you can insert values to database as below:
//Assuming that your id column is first column and name is second column
//get value of id and name
int mId = Convert.ToInt32(GridView2.SelectedRow.Cells[0].Text);
string mName = GridView2.SelectedRow.Cells[1].Text;
string connectionStrng = "your connection string";
string insertSql = "INSERT INTO Machining_Master (M_ID, M_NAME) VALUES (#mId, #mName)";
using (SqlConnection conn = new SqlConnection(connectionStrng))
{
using (SqlCommand cmd = new SqlCommand(insertSql, conn))
{
try
{
cmd.Parameters.Add(new SqlParameter("mId", mId));
cmd.Parameters.Add(new SqlParameter("mName", mName));
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
//Close connection
conn.Close();
}
}
}
I've got a TabContainer control which houses several tabs. Depending on the ActiveIndex property of the TabContainer I would like for a button click to create a Gridview in that tab and bind it to a stored procedure. Currently, the following code works, but there's a lot of code repeated
protected void btnMakeGridView_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
if (TabContainer1.ActiveTabIndex == 0)
{
using (SqlConnection con = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand("spTest0", con))
{
con.Open();
cmd.CommandType = CommandType.Text;
SqlDataReader rdr = cmd.ExecuteReader();
GridView gv = new GridView();
TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
gv.DataSource = rdr;
gv.DataBind();
}
}
}
else if (TabContainer1.ActiveTabIndex == 1)
{
using (SqlConnection con = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand("spTest1", con))
{
con.Open();
cmd.CommandType = CommandType.Text;
SqlDataReader rdr = cmd.ExecuteReader();
GridView gv = new GridView();
TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
gv.DataSource = rdr;
gv.DataBind();
}
}
}
else if (TabContainer1.ActiveTabIndex == 2)
{
using (SqlConnection con = new SqlConnection(cs))
{
using (SqlCommand cmd = new SqlCommand("spTest2", con))
{
con.Open();
cmd.CommandType = CommandType.Text;
SqlDataReader rdr = cmd.ExecuteReader();
GridView gv = new GridView();
TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
gv.DataSource = rdr;
gv.DataBind();
}
}
}
else
{
Label1.Text = "You must select a tab to create the GridView in";
}
}
As you can see depending on the ActiveIndex of the TabContainer, a different stored procedure should be created inside each TabPanel. In my example I'm only working with one database so I don't have to worry about passing the connection string as a parameter for a database to connect to. At first blush I had added something like
//pass in the connection string, the stored procedure name and the ActiveTabIndex
public static void DataAccess(string connectionString,string spName, int activeTabIndex)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(spName, con))
{
con.Open();
cmd.CommandType = CommandType.Text;
SqlDataReader rdr = cmd.ExecuteReader();
GridView gv = new GridView();
gv.DataSource = rdr;
gv.DataBind();
}
}
}
UDPATE: Okay, in the meantime I came up with
public partial class _Default : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnMakeGridView_Click(object sender, EventArgs e)
{
TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(GetData(cs, "spTest", TabContainer1.ActiveTabIndex));
}
public static GridView GetData(string connectionString,string spName, int activeIndex)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(spName, con))
{
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rdr = cmd.ExecuteReader();
GridView gv = new GridView();
gv.ID = "gv" + activeIndex.ToString();
gv.DataSource = rdr;
gv.DataBind();
return gv;
}
}
}
}