populating GridView with sql data - c#

I need to select from my users table the username of the user that has the roleID that I will have to get from the dropdownlist. The data are not appearing in the GridView. Can't see what's wrong, help me please.
Already tried 2 ways
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("select username from tblUser where roleID like '" + DropDownList1.SelectedValue + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("select username from tblUser where roleID like '" + DropDownList1.SelectedValue + "'", con);
SqlDataReader reader = cmd.ExecuteReader();
GridView2.DataSource = reader;
GridView2.DataBind();
con.Close();
}

Okay, so this one worked for me. And you also must check the sources. Like what happened to my GridView, it says AutoGenerateColumns = false, I removed it. And it all worked!
protected void Button2_Click(object sender, EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings["roleDB"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select username from tblUser where roleID like '" + DropDownList1.SelectedValue + "'";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView2.DataSource = dt;
GridView2.DataBind();
con.Close();
}

Related

Searching Form Windows on C# with Data Set

private void textBox7_TextChanged(object sender, EventArgs e)
{
SqlConnection conn = Conn.GetConnection();
conn.Open();
cmd = new SqlCommand("select * from DATA_ITEMS where Items_Name like '" + textBox2.Text + "%'", conn);
ds = new DataSet();
adapter = new SqlDataAdapter (cmd);
adapter.Fill(ds);
dataGridView1.DataSource = ds;
}
I want to ask, why do you want to do a search, but when you type it in the search text box, the data in the grid view disappears (it doesn't appear).
I think you are missing databind in the gridview
private void textBox7_TextChanged(object sender, EventArgs e)
{
SqlConnection conn = Conn.GetConnection();
conn.Open();
cmd = new SqlCommand("select * from DATA_ITEMS where Items_Name like '" + textBox2.Text + "%'", conn);
ds = new DataSet();
adapter = new SqlDataAdapter (cmd);
adapter.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataBind();
}

How to filter results in datagridview combobox based on combobox outside datagridview

I am trying to use the datagridview to input multiple data into sql, but I am having a hard time to bind the datagridview combobox to show items based on the selected item in the combobox (supplier), I can't even bind my sql in the datagridview combobox, sorry for being a noob, but I am trying to finish this project. the code below is the code that I use, it is based on individual comboboxes and textbox and not binded on datagridview.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Inventory.Properties;
namespace Inventory
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(#"Data Source=MEDIXPC197;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cd = new SqlCommand();
private void label15_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'inventoryDataSet.dbTrans' table. You can move, or remove it, as needed.
this.dbTransTableAdapter.Fill(this.inventoryDataSet.dbTrans);
cc();
cc2();
cc3();
}
public void cc()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Department from tbldept";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["Department"].ToString());
comboBox2.Items.Add(dr["Department"].ToString());
}
conn.Close();
}
public void cc2()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select distinct Supplier from tblmaster";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox3.Items.Add(dr["Supplier"].ToString());
}
conn.Close();
}
public void cc3()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [Transaction] from [tbltransac]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox4.Items.Add(dr["Transaction"].ToString());
}
conn.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox2.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tblmaster where Supplier = '" + comboBox3.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
comboBox5.Items.Clear();
foreach (DataRow dr in dt.Rows)
{
comboBox5.Items.Add(dr["ProductCode"].ToString());
}
conn.Close();
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [tbltransac] where [Transaction] = '" + comboBox4.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tblmaster where ProductCode = '" + comboBox5.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
textBox3.Text = dr["Description"].ToString();
textBox4.Text = dr["UM"].ToString();
textBox5.Text = dr["UP"].ToString();
}
conn.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
EDIT:
Picture below shows my design, I want to filter what will show in Item Code combobox inside the datagridview based on the Supplier combobox
PHOTO

How to deleted only 1 rows if next row is the same

private void button3_Click(object sender, EventArgs e)
{
con.Open();
SqlDataAdapter SAD = new SqlDataAdapter("DELETE FROM Table1 WHERE ID_='"+textBox4.Text.ToString() + "'", con);
SAD.SelectCommand.ExecuteNonQuery();
MessageBox.Show("DELETE ALREADY!");
string query = "SELECT * FROM Table1";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
SDA.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
I think you want to delete duplicates but keep only one.
Can you try this ?
DELETE FROM Table1
WHERE ID_ NOT IN
(
SELECT ID_
FROM Table1
)

Fetch boolean value from database

protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=ENTERKEY001;Initial Catalog=ContactManagement;Integrated Security=True");//DataBase Connection
String NAME = TextBox4.Text;
SqlCommand getID = new SqlCommand("SELECT ID FROM UserDetailsOne WHERE NAME='" + NAME + "'", con);
con.Open();
SqlDataReader dr = getID.ExecuteReader();
if (dr.Read())
{
String ID = dr[0].ToString();
SqlCommand getBLOCK = new SqlCommand("SELECT BLOCK FROM UserDetailsTwo WHERE ID='" + ID + "'", con);
dr.Close();
SqlDataReader dr0 = getBLOCK.ExecuteReader();
if (dr0.Read())
{
String BLOCK = dr0[0].ToString();
if (BLOCK == "false")
{
SqlCommand cmd = new SqlCommand("select NAME,UserDetailsOne.ID,ADDRESS,GENDER,MOBILENO,PHOTO from UserDetailsOne left join UserDetailsTwo on UserDetailsOne.ID=UserDetailsTwo.ID where NAME like #NAME", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
else
{
Response.Redirect("1.aspx");
}
}
}
}
if(Convert.ToBoolean(tablename.Rows[index row][index columns])=true)
{
//do sth
}
one way to access to table is index of columns or if you want to find with columns name you can write name "" in []

sql search query in 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();
}

Categories

Resources