I am working on a winforms project and i have this following code in the Form_Load method. But it doesnt work. Can anyone help me?
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Sella.Properties.Settings.Database1ConnectionString1"].ConnectionString);
// A SqlCommand object is used to execute the SQL commands.
SqlCommand scmd = new SqlCommand("Select * From CustCalls", conn);
// A SqlDataAdapter uses the SqlCommand object to fill a DataSet.
SqlDataAdapter sda = new SqlDataAdapter(scmd);
// Create and Fill a new DataSet.
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds;
Try sourcing directly to the table in the dataset:
dataGridView1.DataSource = ds.Tables[0];
SqlDataAdapter sda = new SqlDataAdapter(scmd, conn);
dataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
Related
Hi i want to save edits in DataGriView to datatable , i tried that code but an error shows 'System.ArgumentOutOfRangeException: 'The index was out of range. It must not be negative and must be smaller than the size of the collection.
Parameter name: index '' help
private void button11_Click(object sender, EventArgs e)
{
con.Open();
String query = "SELECT * FROM Taom";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt1 = new DataTable();
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
dataGridView1.DataSource = dt1;
SDA.Fill(dt);
dataGridView1.Rows[2].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[5].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[6].Cells[2].Value)));
dataGridView1.Rows[3].Cells[2].Value = Math.Atan((Convert.ToDouble(dataGridView1.Rows[7].Cells[2].Value)) / (Convert.ToDouble(dataGridView1.Rows[8].Cells[2].Value)));
ds1.Tables.Add(dt);
con.Close();}
I changed my code to that code no errors showed after i run values on datagridview change but no changes in datatable !!!
string query = "SELECT * FROM [dbo].[Taom]";
SqlConnection conn = new SqlConnection(#"Data Source=STE-P0024818PW;Initial Catalog=test;Integrated Security=True");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Contents"] = "98"; //Before it was 10
dt.Rows[1]["Contents"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
Fill the right value inside your table and after pass the table rightly filled in your datasource, don't change this directly in the gridview like:
I try myself and it works:
private void Run()
{
string query = "SELECT * FROM dbo.[Anrufthema]";
SqlConnection conn = new SqlConnection("MyConnectionString");
conn.Open();
SqlDataAdapter SDA = new SqlDataAdapter(query, conn);
DataSet ds1 = new DataSet();
DataTable dt = new DataTable();
SDA.Fill(dt);
dt.Rows[0]["Anrufthema"] = "98"; //Before it was 10
dt.Rows[1]["Anrufthema"] = "99"; //Before it was 11
ds1.Tables.Add(dt);
conn.Close();
}
My Result, it works !
Here what I have done
I make a GridView and choose the data key name as id and in the basis of id I want to show the DetailsView. Here the CS code
using (SqlConnection con1 = new SqlConnection("Data Source= IA; initial catalog =aip; integrated Security=true;"))
{
con1.Open();
SqlCommand cmd = new SqlCommand("select * from Pm where user_id='" +(String)Session["uid"]+ "'", con1);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con1.Close();
}
Then I make DetailsView method on SelectedIndexChanged:
But it is showing empty DetailsView in output on selecting 'Select' option
here is the code image
enter image description here
You have used the DataSet not DataTable. So you could either do this:
da.Fill(ds,"tbl");
GridView1.DataSource = ds.Tables[0];
Or use a DataTable instead:
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
Also you should always use parameterized queries to avoid SQL Injection. Something like this:
SqlCommand cmd = new SqlCommand("select * from Pm where user_id=#userId", con1);
cmd.Parameters.AddWithValue("#userId", (String)Session["uid"]);
Also have a look at this: Can we stop using AddWithValue() already?
you need to set DataTable instead of DataSet
GridView1.DataSource = ds.Tables["Pm"];
#PIYUSH Itspk please check query and replace
SqlCommand cmd = new SqlCommand("select * from Pm where user_id=" +Session["uid"].tostring()+ ", con1);
i think its help you if any other query please notify me
I need some help because I've been trying different things but nothing seems to work properly the question itself is the one below.
How can I bind data to a grid-view in Visual Studio 2013 with a local SQL Server database using the code behind C# ?
Here is the C# I'm trying:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Guillermo\Desktop\HorsensHospital\App_Data\HospitalDB.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.ExecuteNonQuery();
con.Close();
}
You just assing your empty DataSet to your Gridview's DataSource.
You need to use .Fill method fo fill your DataSet of SqlDataAdapter. You don't need to use ExecuteNonQuery. This method just executes your query. It doesn't return any data or something as a result.
Also use using statement to dispose your SqlConnection, SqlCommand and SqlDataAdapter. You don't need to .Close() your database connections and objects when you use it.
using(SqlConnection con = new SqlConnection(conString))
using(SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]";
using(SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
You should use statement like this :
when you use SqlDataAdapter you should fill an dataset with Fill Method
then set the DataSource Properties Of GridView
SqlCommand cmd = new SqlCommand("SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
You have missed Fill method to fill your your dataset that is why i think you are not able to display gridview.
Fill Method is very Improtant method Bcoz whatever your query is returning that should be filled into your dataset & then that dataset is binded by your Gridiview
Hope this will help you
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Guillermo\Desktop\HorsensHospital\App_Data\HospitalDB.mdf;Integrated Security=True;Connect Timeout=30");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT [Task_temp_name], [Task_templatesID] FROM [Task_templates]", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.ExecuteNonQuery();
con.Close();
}
Note : You need to create your connection string in Web.Config. Bcoz If you have created your connection string there then you will not need to create that again. Once creating there you will just need to call that from the Web.config
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringNameFromWebConfig"].ConnectionString);
This is the syntax for calling connection string from web.config
I'm working on a webpage in azure. Here's a part of my code:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataTable ds = new DataTable();
_adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Off course that instead of "my connection string" there's the real one...
The problem is that the page loads but without the gridview for some reason...
Any help will be great, Thanks
EDIT: I also tried with datasets, like this:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
_adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
Try with GridView1.DataSource = ds.Tables[0];
whole code will look as follows:
using (SqlConnection conn = new SqlConnection("my connection string"))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Students", conn);
conn.Open();
SqlDataAdapter _adp = new SqlDataAdapter(cmd);
DataSet ds = new DataTable();
_adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
I have the code below which has multiple DataTables with results and I need to pass a single DataSet with all DataTable values.
MySqlCommand cmd = new MySqlCommand(getLikeInfo, con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
MySqlCommand cmd1 = new MySqlCommand(getKnowInfo, con);
MySqlDataAdapter da1 = new MySqlDataAdapter(cmd1);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
MySqlCommand cmd2 = new MySqlCommand(getHotelInfo, con);
MySqlDataAdapter da2 = new MySqlDataAdapter(cmd2);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
MySqlCommand cmd3 = new MySqlCommand(getRoomInfo, con);
MySqlDataAdapter da3 = new MySqlDataAdapter(cmd3);
DataTable dt3 = new DataTable();
da3.Fill(dt3);
MySqlCommand cmd4 = new MySqlCommand(getFoodInfo, con);
MySqlDataAdapter da4 = new MySqlDataAdapter(cmd4);
DataTable dt4 = new DataTable();
da4.Fill(dt4);
How can I do that?
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
...
If you want your tables named in the DataSet you can create your tables from the DataSet
DataSet ds = new DataSet();
DataTable t1 = ds.Tables.Add("t1"); // Create
DataTable t1Again = ds.Tables["t1"]; // fetch by name
You can also set the TableName property of the tables if you use the first approach.