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
Related
So I am using MS visual studio to create an application in c# that will pull information from a sql server database.
I have created a textbox and a button to search my gridview. I am using a stored procedure that searched multiple rows to pull information from my Sql Database.
I am having trouble with my aspx.cs code. I have tried so many different ways to create a searchbox but haven't had any luck yet
Here is my code for my search button.
I am getting the error-
"Input string was not in a correct format."
this error is on the line cmd.ExecuteNonQuery();
Help is much appreciated, thank you.
protected void Button_srch_invest1_Click(object sender, EventArgs e)
{
string connectionStr = ConfigurationManager.ConnectionStrings["ORAProjectConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionStr))
{
string find = "sp_SrcProtocols";
SqlCommand cmd = new SqlCommand(find, con);
cmd.Parameters.Add("#ORAID", SqlDbType.Int).Value = TextBox_Srch.Text;
cmd.Parameters.Add("#InvestLastName", SqlDbType.NVarChar).Value = TextBox_Srch.Text;
cmd.Parameters.Add("#ManagerLastName", SqlDbType.NVarChar).Value = TextBox_Srch.Text;
con.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "ORAID");
da.Fill(ds, "InvestLastName");
da.Fill(ds, "ManagerLastName");
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}
By default, a SqlCommand expects a query, not a stored procedure's name. You have to set the command type before executing it.
cmd.CommandType = CommandType.StoredProcedure;
It seems, you are passing same text box value (TextBox_Srch.Text) to all 3 parameters. And first parameter #ORAID is expecting integer value and you might be passing text. So it's causing SQL server to raise below error.
Input string was not in a correct format.
This is what worked (and i changed my sql to just accept one parameter #search)
protected void Button_srch_invest1_Click(object sender, EventArgs e)
{
string connectionStr = ConfigurationManager.ConnectionStrings["ORAProjectConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "sp_SrcProtocols";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#search", SqlDbType.NVarChar).Value = TextBox_Srch.Text;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
con.Close();
}
}
I'm importing the table 'tblTable' to Datagridview in C# with:
private void button1_Click(object sender, EventArgs e)
{
con.Open();
//
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM tblTable";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sa = new SqlDataAdapter(cmd);
sa.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
I have written a specific SQLQuery, 'SQLQuery1.sql' that does some groupings from the table 'tblTable' that I'm already importing. What would be the best way to import only the SQLQuyery1.sql in to C#, without importing all the table?
Many thanks
It would probably be best to make your query into a Stored Procedure, and call instead:
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "MyStoredProc";
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;
}
}
I am using SQL Server 2014 and Visual Studio 2013. I want to write C# code so that user should check checkbox and select an item from combobox then this item will be sent to a stored procedure in SQL Server. In real I want to send parameter to stored procedure from combobox. Code below written by me but does not work well.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked) ;
SqlConnection con = new SqlConnection(#"Data Source=USER-PC\----);
SqlCommand Cmd = new SqlCommand("select_Info_Person", con);
Cmd.CommandType = CommandType.StoredProcedure;
con.Open();
DataTable dt = new DataTable();
using(SqlDataAdapter da = new SqlDataAdapter(Cmd))
{
da.Fill(dt);
comboBox1.DisplayMember = "Person_Expert";
comboBox1.DataSource = dt;
con.Close();
}
}
No problem, if SP is without any parameter. It seems your connection string is not properly initialized, otherwise no problem at all. Check the Error type and share. Otherwise OK.
It seems that you are not sending any parameter to the Stored procedure
Step 1:
Declare Parameter in your storedprocedure and use it in where condition..
CREATE PROCEDURE select_Info_Person
-- Add the parameters for the stored procedure here
#p1 int = 0
AS
BEGIN
-
-- Insert statements for procedure here
Select * from PersonMaster where personID=#p1
END
GO
Then in the code
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox.Checked) ;
SqlConnection con = new SqlConnection(#"Data Source=USER-PC\----");
SqlCommand Cmd = new SqlCommand("select_Info_Person", con);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.AddWithValue("#p1", comboBox1.SelectedValue.ToString());
// or if you dont have value feild
// Cmd.Parameters.AddWithValue("#p1", comboBox1.Selecteditem.Text.ToString());
con.Open();
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
{
da.Fill(dt);
//I cannot understand what you are doing using below
// comboBox1.DisplayMember = "Person_Expert";
// comboBox1.DataSource = dt;
// con.Close();
}
}
if (!IsPostBack)
{
OracleConnection conn = new OracleConnection(conns); // C#
OracleCommand cmd = new OracleCommand("select * from APPLICATION AND FRAME", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
OracleDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
grdSeacrhResult.DataSource = dt;
grdSeacrhResult.DataBind();
conn.Close();
}
got error of
ORA-00933: SQL command not properly ended.
Once you have sorted your SQL out use Nuget and get closedXML,
then your code should be
private void WriteExcelData(string f, DataTable dt, string savePath, string sheetName)
{
using (wb == new XLWorkbook(f))
{
dynamic ws = wb.Worksheet(sheetName);
ws.Cell(1, 1).InsertTable(dt.AsEnumerable());
wb.SaveAs(savePath);
}
}
or if you want to use the closed XML documentation use this https://closedxml.codeplex.com/wikipage?title=Adding%20DataTable%20as%20Worksheet&referringTitle=Documentation