I'm trying to establish a connection to a local SQL server using this code:
dataGridView1.Visible = true;
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=.;Initial Catalog=Form;Integrated Security=True";
SqlCommand com = new SqlCommand("select * from Form", con);
SqlDataAdapter sda = new SqlDataAdapter(com);
dataGridView1.DataSource = sda;
But when I press the button in my form it doesn't show me the data! What am i doing wrong?
you need something like this
dataGridView1.Visible = true;
SqlConnection con = new SqlConnection();
con.ConnectionString = #"Data Source=.;Initial Catalog=Form;Integrated Security=True";
con.Open()
SqlCommand com = new SqlCommand("select * from Form", con);
SqlDataAdapter sda = new SqlDataAdapter(com);
DataTable dt=new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;//set it to datatable
dataGridView1.DataBind();
You need to open the connection with con.Open();.
con.ConnectionString = #"Data Source=.;Initial Catalog=Form;Integrated Security=True";
SqlCommand com = new SqlCommand("select * from Form", con);
con.Open();
Also you had a lot of other problems like you don't dispose your resource which can lead to memory leak and not closing your connection, also you are not filling your adapter. Here is optimal way to doing it.
using(SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Form;Integrated Security=True";))
{
con.Open();
SqlCommand com = new SqlCommand("select * from Form", con);
using(SqlDataAdapter sda = new SqlDataAdapter(com));
{
DataTable resultTbl = new DataTable();
sda.Fill(resultTbl);
dataGridView1.DataSource = resultTbl;
dataGridView1.DataBind();
}
}
Related
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);
}
Basically I am trying to develop a software and I am new in programming. I am trying to insert the data of textbox into SQL Server 2008 R2 Standard and I am getting an error:
System.NullReferenceException was unhandled
Here is my code.
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";
con.Open();
DataSet ds = new DataSet();
String sql = "Select * From tbl_songdb";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataRow drow = ds.Tables["tbl_songdb"].NewRow(); // I am getting error message here.
drow[1] = txt_songName.Text;
drow[2] = txt_minute.Text;
drow[3] = txt_albumnName.Text;
drow[4] = txt_location.Text;
ds.Tables["tbl_songdb"].Rows.Add(drow);
con.Close();
actually my dataset was empty, thats why it was showing NULL error
da.Fill(ds, "tbl_studentData");
i used these lines to fill it and now everything is working fine.
thanks to all for giving their time.
Just do exactly what error said to you. Handle it with try catch like that:
try{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";
con.Open();
DataSet ds = new DataSet();
String sql = "Select * From tbl_songdb";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataRow drow = ds.Tables["tbl_songdb"].NewRow(); // I am getting error message here.
drow[1] = txt_songName.Text;
drow[2] = txt_minute.Text;
drow[3] = txt_albumnName.Text;
drow[4] = txt_location.Text;
ds.Tables["tbl_songdb"].Rows.Add(drow);
da.Update(ds);
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=(local);Initial Catalog=songs_db;Persist Security Info=True;User ID=sa;Password=iloveyourb";
con.Open();
DataSet ds = new DataSet();
String sql = "Select * From tbl_songdb";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
DataRow drow = ds.Tables[0].NewRow(); // I am getting error message here.
drow[1] = txt_songName.Text;
drow[2] = txt_minute.Text;
drow[3] = txt_albumnName.Text;
drow[4] = txt_location.Text;
ds.Tables[0].Rows.Add(drow);
SQLiteCommandBuilder cmdbuilder = new SQLiteCommandBuilder(da);
da.InsertCommand = cmdbuilder.GetInsertCommand();
da.Update(ds);
ds.AcceptChanges();
con.Close();
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 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 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.