I have a textbox and i want to write the query in textbox and show the result in datagridview.
if i write something like this Select * From Login in textbox and click on button to show the result in datagridview.
i have tried this code but unable to do it.
SqlConnection con = new SqlConnection(#"Data Source=LAPTOP-A56NKPB9\SQLSERVER;Initial Catalog=DB_EMS;Integrated Security=True");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("'" + query.Text + "'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dgv.DataSource = dt;
I know it's a bad idea but i need to complete this task.
Related
I want to get specific colums to show on my C# WPF DataGrid.
I used this code to get selected columns:
using (SqlConnection con = new SqlConnection(ConString))
{
SqlCommand cmd = new SqlCommand("SELECT roll FROM cmt_7th", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("cmt_7th");
sda.Fill(dt);
MydataGrid_roll.ItemsSource = dt.DefaultView;
}
But I want to show only those row which column data is empty .
Left image is output screen short and right image is sql table image on "Like this" link image
I want to get rows 5 to 10 and ignore 1 to 4 row where all columns are not null.
According to your description, you want to get all row where name, department, phone are null. So, you have to apply condition in your sql. Please check this:
string ConString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConString))
{
SqlCommand cmd = new SqlCommand("SELECT roll FROM cmt_7th WHERE name IS Null And department IS Null And phone IS Null", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("cmt_7th");
sda.Fill(dt);
MydataGrid_roll.ItemsSource = dt.DefaultView;
}
Check this output of the SQL:
Sorry if this is a stupid question! I'm very new to coding and only have limited access to the internet whilst at work, I was just wondering if this is possible? and If so do you have a useful link for me to read over?
A quick overview of what I'm trying to do.
I have form1 and form2, form1 has a datagridview box that pulls data from the database, when I click on a cell it opens form2, which has a checklistbox which I want to now put code in so it pulls the data from a specific column and checks a box if the cell that was clicked applies to it.
Go easy on me if this question is stupid ^^, thanks in advance!
Some code I have produced so far! Which pulls the boxes from the database, but I also need to check the boxes too..
SqlConnection con = new SqlConnection("=");
con.Open();
string query = "select ID from dbo.report";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda;
DataSet ds = new DataSet();
sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
DataTable dt = ds.Tables[0];
foreach (DataRow datarow in dt.Rows)
{
checkedListBox1.Items.Add(datarow["ReportID"]);
Edited: put current coding in.
You can use below code to bind checkboxlist from database.
SqlConnection con = new SqlConnection("=");
con.Open();
string query = "select ID,NAME from dbo.report";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter sda;
DataSet ds = new DataSet();
sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
checkedListBox1.DataValueField = "ID"
checkedListBox1.DataTextField = "NAME"
checkedListBox1.DataSource = ds.Tables[0];
checkedListBox1.DataBind();
Hello internet so first i am newbie at c# and i am doing project about changing data from a sql table. I passed the result of the query to the datatable and datagridview, but what i want next is i want the modified datagridview table passing to a dataset. this is how i get my table from the sql:
string query = "select Article,Barcode,\"Minimum Stock\",\"Current Stock\" from article where Fid=2 and \"Minimum Stock\" > \"Current Stock\"";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
da.Dispose();
Now the users will modifie the table and valid the information. How can i save it to a dataset?
I tried to bind dataset to the datagridview:
Dataset1.Tables.Add(datagridview1.datasource);
But it threw a error where it says datasource doesn't use here
Can anyone help?
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();
This one is a mystery for me, I know the code I took it from others, in my case the datatable it returns is empty
conSTR is the connection string, set as a global string
public DataTable fillDataTable(string table)
{
string query = "SELECT * FROM dstut.dbo." +table;
SqlConnection sqlConn = new SqlConnection(conSTR);
sqlConn.Open();
SqlCommand cmd = new SqlCommand(query, sqlConn);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
sqlConn.Close();
return dt;
}
EDIT 1
The whole point is to later show this table in a datagrid view on a tabcontrol, here is the question on that
displaying multiple datatable in tabcontrol C#
Here it just show's me a blank datagridview
EDIT 2
Tried them all, when I try to display the table, the datagridview is empty, have the right amount of rows but now value
If the variable table contains invalid characters (like a space) you should add square brackets around the variable.
public DataTable fillDataTable(string table)
{
string query = "SELECT * FROM dstut.dbo.[" + table + "]";
using(SqlConnection sqlConn = new SqlConnection(conSTR))
using(SqlCommand cmd = new SqlCommand(query, sqlConn))
{
sqlConn.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
return dt;
}
}
By the way, be very careful with this kind of code because is open to Sql Injection. I hope for you that the table name doesn't come from user input
Try with following:
public DataTable fillDataTable(string table)
{
string query = "SELECT * FROM dstut.dbo." +table;
SqlConnection sqlConn = new SqlConnection(conSTR);
sqlConn.Open();
SqlCommand cmd = new SqlCommand(query, sqlConn);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
sqlConn.Close();
return dt;
}
Hope it is helpful.