This code is working fine but when i search id like 121452, so it will call all numbers that have "1" or "2" in db. so i want it show only the exact id what i search
private void btn_search_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\acap\Documents\Data.mdf;Integrated Security=True;Connect Timeout=30");
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Student where No_ic = " + boxSearch.Text, con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
//dataGridView1.Visible = true;
}
Try this :
Updated: (Previously i forgot to bind dataset.)
private void btn_search_Click(object sender, EventArgs e) {
String bResult = boxSearch.Text;
string connectionString = "Data Source=.;Initial Catalog=sacbase;Integrated Security=True"; // add your conncetion string here
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT * FROM Student where No_ic =#val", connection);
cmd.Parameters.AddWithValue("#val", bResult);
SqlDataAdapter dataadapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "student_table");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "student_table";
}
Related
I want to use two DataGridViews in this form, which will receive their information from two different tables in one database. But when I run the program, both DataGridViews only display the second table information.
private void Ring_Load(object sender, EventArgs e)
{
showdata();
showmedal();
}
void showdata()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
da.SelectCommand = cmd;
dt.Clear();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[3].Visible = false;
}
void showmedal()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
da.SelectCommand = cmd;
dt.Clear();
da.Fill(dt);
dataGridView2.DataSource = dt;
}
private void Ring_Load(object sender, EventArgs e)
{
showdata();
showmedal();
}
void showdata()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
da.SelectCommand = cmd;
using(DataTable dt = new DataTable())
{
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
dataGridView1.Columns[3].Visible = false;
}
}
void showmedal()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
da.SelectCommand = cmd;
using(DataTable dt = new DataTable())
{
da.Fill(dt);
dataGridView2.DataSource = dt;
dataGridView2.DataBind();
}
}
Try this
private void Ring_Load(object sender, EventArgs e)
{
showdata();
showmedal();
}
void showdata()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
da.SelectCommand = cmd;
dt.Clear();
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Columns[3].Visible = false;
}
void showmedal()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
da.SelectCommand = cmd;
dt.Clear();
dt = new DataTable();
da.Fill(dt);
dataGridView2.DataSource = dt;
}
You seem to be reusing da and dt. Reusing da is no problem, but reusing dt is. When you assign dt to DataGridView.DataSource, the data is NOT copied! So in the end, both DataGridViews will be using the same DataTable object, that holds the data from the second table (medal).
You could try this:
private void Ring_Load(object sender, EventArgs e)
{
showdata();
showmedal();
}
void showdata()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM Ring", con);
da.SelectCommand = cmd;
DataTable dt1 = new DataTable();
da.Fill(dt1);
dataGridView1.DataSource = dt1;
dataGridView1.Columns[3].Visible = false;
}
void showmedal()
{
SqlConnection con = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("SELECT Number,Weight,Ring_Id FROM medal", con);
da.SelectCommand = cmd;
DataTable dt2 = new DataTable();
da.Fill(dt2);
dataGridView2.DataSource = dt2;
}
Edit: renamed the local DataTable variables for clarity.
I wanat to retrieve student ID from database. Here is my code what i have tried but it is not displaying anything.
Thanks...
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-Q69PRF4;Initial Catalog=new;Integrated Security=True");
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
string str = "select * from StRecords where StID='" + Session["login"] + "'";
SqlCommand com = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Label11.Text = dt.Rows[0]["StID"].ToString();
}
}
If you want to take just StudentId from the database, then you just select that column and use ExecuteScalar property.
Code
protected void Button2_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("Data Source=DESKTOP-Q69PRF4;Initial Catalog=new;Integrated Security=True"))
{
con.Open();
string str = "select [StID] from StRecords where StID = #stdId;";
using (SqlCommand com = new SqlCommand(str, con))
{
com.Parameters.AddWithValue("#stdId", Session["login"]);
Label11.Text = com.ExecuteScalar().ToString();
}
}
}
Also always use parameters instead passing the value in single quotes to avoid SQL injection attack.
Also try to give the connection string in Web.Config file.
I have a GridView. I am trying to display DB table data into grid using DataTable. So I am saving the query result in DataTable, but the data is not displayed. Here is my code. Please help.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dataTable = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["gridconnection"].ConnectionString;
string query = "select * from GridExcel";
SqlConnection con1 = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query, con1);
con1.Open();
// create data adapter
SqlDataAdapter da = new SqlDataAdapter(cmd);
// this will query your database and return the result to your
datatable
da.Fill(dataTable);
Gridview1.DataSource = dataTable;
Gridview1.DataBind();
ViewState["CurrentTable"] = dataTable;
con1.Close();
}
}
Try to use a DataReader:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dataTable = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["gridconnection"].ConnectionString;
string query = "select * from GridExcel";
SqlConnection con1 = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query, con1);
con1.Open();
// create data adapter
SqlDataReader reader = cmd.ExecuteReader();
// this will query your database and return the result to your datatable
dataTable.Load(reader);
Gridview1.DataSource = dataTable;
Gridview1.DataBind();
ViewState["CurrentTable"] = dataTable;
con1.Close();
}
}
The below code is works for me.
string constring = #"Data Source=.\SQL2005;Initial Catalog=gridconnection;";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("select * from GridExcel", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
Why do I get blank rows when I retrieve rows from MySQL database to dataGridView? I end up getting the amount of rows but its empty (it has no text).
this is my code so far:
private void button2_Click(object sender, EventArgs e)
{
string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand("Select * FROM TopShineDB.Table1 ;", conDataBase);
using (MySqlConnection conn = new MySqlConnection(constring))
{
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dt = new DataTable();
sda.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
sda.Update(dt);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
I tried below code sample to get data from database and bind to datagridview.
var ConnectionString = "your ConnectionString";
MySqlConnection connect = new MySqlConnection(ConnectionString);
MySqlCommand cmd = new MySqlCommand("your query");
connect.Open();
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
DataTable dt = new DataTable();
dt = DtSet.Tables[0];
dataGridView1.DataSource = DtSet.Tables[0];
connect.Close();
Try This:
private void BindGrid()
{
string conString = #"Data Source=localhost;port=3306;Initial Catalog=TopShineDB;User Id=root;password=0159";
using (MySqlConnection con = new MySqlConnection(conString))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM Table1", con))
{
cmd.CommandType = CommandType.Text;
using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
}
}
*Just Clear your Columns you define in DataGridView *
private void button2_Click(object sender, EventArgs e)
{
string constring = "Data Source = localhost; port = 3306; username = root; password = 0159";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand("Select * FROM TopShineDB.Table1 ;", conDataBase);
using (MySqlConnection conn = new MySqlConnection(constring))
{
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.Columns.Clear();
dataGridView1.DataSource = bs;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
c#
I'm trying to make a database search in my app where the user would choose the column and enter the search word and the result would come up in a dataviewgrid.
This is the code i've been working on, the problem is that nothing comes up and i'm pretty sure there are entries in the database. EDIT : it's a windows form application
private void button1_Click(object sender, EventArgs e)
{
conn = new SqlConnection("Server = localhost; database = Clients; Integrated Security = SSPI");
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * From dbo.Tclients WHERE #choice = #input", conn);
cmd.Parameters.AddWithValue("#choice", comboBox1.Text);
cmd.Parameters.AddWithValue("#input", textBox1.Text);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
conn.Close();
}
You cannot use a parameter to express the name of a column.
You should populate your combobox with the column names and set its DropDownStyle property to DropDownList (do not allow your user to type the name of the column) and then build your query
private void button1_Click(object sender, EventArgs e)
{
string cmdText = "SELECT * From dbo.Tclients WHERE " + comboBox1.Text + " = #input";
using(SqlConnection conn = new SqlConnection(....))
using(SqlCommand cmd = new SqlCommand(cmdText, conn))
{
conn.Open();
cmd.Parameters.AddWithValue("#input", textBox1.Text);
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
}
you forgot to bind the grid view with datasource
add this after data source
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.DataBind();
private void bunifuThinButton21_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = (#"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\albasheer\Desktop\games\my_school\my_school\school.mdf;Integrated Security=True;User Instance=True");
connection.Open();
string sql = "select name,id,stage,age,cost from STUDENT where stage like '%" + bunifuCustomLabel1.Text + "%' and name like '%" + bunifuMaterialTextbox1.Text + "%'";
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
SqlCommand command = new SqlCommand(sql, connection);
DataTable table = new DataTable();
adapter.Fill(table);
var dt = from t in table.AsEnumerable()
select new
{
id = t.Field<int>("id"),
Name = t.Field<string>("name"),
};
bunifuCustomDataGrid1.DataSource = dt.ToList();
}