Hi i have combobox with Table names. I would like to display selected table on button click. How to do it? This is code of my combobox
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT name FROM sys.tables", Con);
SqlDataReader rdr;
rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Load(rdr);
wczytywanie.ValueMember = "name";
wczytywanie.DataSource = dt;
Con.Close();
now i would like to display these tables in dataGridView after button click.This is not working.
Con.Open();
SqlCommand cmd = new SqlCommand("SELECT * from sys.tables where name=#name", Con);
cmd.Parameters.Add("name", wczytywanie.SelectedValue.ToString());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Dane.DataSource = dt;
Con.Close();
try adding DisplayMember and assign value "name"
wczytywanie.ValueMember = "name";
wczytywanie.DisplayMember = "name";
This might do
conn = new MySqlConnection(connectString);
conn.Open();
comm = new MySqlCommand(query, conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
DataSet DS = new DataSet();
adapter.Fill(DS);
advancedDataGridView1.DataSource = DS.Tables[0];
conn.Close();
And to change the column name your sql be like columnname as 'your preferred column name'
SELECT principal_id as 'Principal ID' from sys.tables where name=#name"
Related
I want to retrieve data from postgres db into textbox in C# without using grid.
Here is the running successful code by using grid which I had tried:
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = "SELECT * FROM student_folio";
cmd.CommandType = CommandType.Text;
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
cmd.Dispose();
connection.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
I get error when build when I want to retrieve it into textbox:
"cannot apply indexing with [] to an expression of type 'NpgsqlDataAdapter'"
Here is my block of code:
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;
cmd.CommandText = ("SELECT f_name FROM student_folio WHERE id = 1");
cmd.CommandType = CommandType.Text;
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
txtFname.Text = da["f_name"].ToString();
cmd.ExecuteNonQuery();
cmd.Dispose();
connection.Close();
A DataAdapter is not an array of rows you can loop into.
Look at your first code block : you must fill a DataTable from your adapter and then read through the Rows property of this DataTable.
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
txtFname.Text = dt.Rows[0]["f_name"].ToString();
}
You could also do this :
foreach (System.Data.DataRow row in dt.Rows)
{
txtFname.Text = row["f_name"].ToString();
}
And please, remove the cmd.ExecuteNonQuery(); line, it is useless here
try this . .
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
NpgsqlDataReader dr=null;
cmd.Connection = connection;
cmd.CommandText = ("SELECT f_name FROM student_folio WHERE id = 1");
cmd.CommandType = CommandType.Text;
dr=cmd.ExecuteReader();
while(dr.HasRows)
{
while(dr.Read())
{
txtFname.Text = da["f_name"].ToString();
}
}
connection.Close();
I have datagridview, that i must fill by 5 tables. I declared SqlCommand and SqlConnection.
After that I use somethine like this:
selCommand.Connection = conn;
dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter();
ad.SelectCommand = selCommand;
ad.Fill(dt);
dataGridView1.DataSource = dt;
As a result I have column headers of my query in datagridview, but don't have data.
I tried use this code:
selCommand.Connection = conn;
dt = new DataTable();
SqlDataReader dr = selCommand.ExecuteReader();
dt.Load(dr);
bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
dr.Close();
It was working, but I something change and I can't understand why it does not work.
Try this:
DataTable table = null;
using (SqlConnection connection = new SqlConnection(this.connectionString))
{
try
{
connection.Open();
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "SELECT * FROM Something WHERE Id = #Id";
cmd.Parameters.Add(new SqlParameter("#Id", YourValue));
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
table = new DataTable();
adapter.Fill(table);
}
}
catch (Exception ex)
{
//Handle your exception;
}
}
dataGridView1.DataSource = table;
From the title above, Im making a windows form in c# where in 2 of my checkedlistbox should populate the values from 2 tables in the database. However, when I tried to run the code, I always get this exception: "Cannot bind to the new value member".
below is my code. if you could find any errors/suggestions, feel free to comment. It would be a big help. thanks!
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataTable dt2 = new DataTable();
string connstring = ("Server=localhost;Port=5432;User Id=postgres;Password=021393;Database=postgres;");
NpgsqlConnection conn = new NpgsqlConnection(connstring);
NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM data_condition", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd);
dt.TableName = "data_condition";
da.Fill(dt);
checkedListBox1.DataSource = dt;
checkedListBox1.DisplayMember = "conname";
checkedListBox1.ValueMember = "conid";
string[] condition = dt.Rows[0]["conname"].ToString().Split(',');
NpgsqlCommand cmd2 = new NpgsqlCommand("SELECT * FROM data_allergy", conn);
cmd2.CommandType = CommandType.Text;
NpgsqlDataAdapter da2 = new NpgsqlDataAdapter(cmd2);
dt2.TableName = "data_allergy";
da2.Fill(dt2);
checkedListBox2.DataSource = dt2;
checkedListBox2.DisplayMember = "allergyname";
checkedListBox2.ValueMember = "allerygid";
string[] allergy = dt2.Rows[0]["allergyname"].ToString().Split(',');
I have created a form in asp.net,where i have some textbox, in which I have taken some input which is stored in the database. I have created another page where I have taken the same number of label as the textbox.
I want to show the data in the labels that I last entered.
How can i do that?
Code:
string cs = ConfigurationManager.ConnectionStrings["TrishanConnection"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 1 coil_id FROM CoilDetails ORDER BY coil_id DESC", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
LabeCoilid.Text = ds.ToString();
LabeCoilid.DataBind();
con.Close();
Try instead of
LabeCoilid.Text = ds.ToString();
this
LabeCoilid.Text = ds.Tables[0].Rows[0][0].ToString();
LabeCoilid.Text = ds.Tables[0].Rows[0]["coil_id"].ToString();
You don't need to call DataBind()
string cs = ConfigurationManager.ConnectionStrings["TrishanConnection"].ConnectionString;
using(SqlConnection con = new SqlConnection(cs))
{
using (SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 1 coil_id FROM CoilDetails ORDER BY coil_id DESC", con))
{
con.Open();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(ds);
dt=ds.Table[0]
}
}
LabeCoilid.Text = dt.Rows[0][0].ToString();
LabeCoilid.DataBind();
con.Close();
You can change the rows and columns by changing dt,Rows[4][8]
SqlConnection con = getConnection();
SqlDataAdapter da = new SqlDataAdapter(storedprocedureName, con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
con.Open();
DataSet DS = new DataSet();
da.Fill(DS);
stateDropDownList.DataSource = DS.Tables[0];
stateDropDownList.ValueMember = "STATE_ID";
stateDropDownList.DisplayMember = "STATE_NAME";
This data source is returning state details and i have attached the satedetails datasource into combobox.
i want to display the combobox with first item select and rest as all states.
i googled it a lot for dropdown list there are codes but nothing showing ...
It can be done in the same way as Drop Down list:
ComboBox1.Items.Insert(0, "--Select--");
Entire code:
SqlConnection con = getConnection();
SqlDataAdapter da = new SqlDataAdapter(storedprocedureName, con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
con.Open();
DataSet DS = new DataSet();
da.Fill(DS);
ComboBox1.DataSource = DS.Tables[0];
ComboBox1.ValueMember = "STATE_ID";
ComboBox1.DisplayMember = "STATE_NAME";
ComboBox1.Items.Insert(0, "--Select--");