using dropdown menu sql statement not working - c#

protected void btn_search_Click(object sender, EventArgs e)
{
using (con = new SqlConnection(CS))
{
string _var_search = ddl_search_by.SelectedItem.Text;
string _var_by = ddl_search.SelectedItem.Text;
cmd = new SqlCommand("Select * from UserProfile Where '"+_var_search+"'='" + _var_by + "'", con);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
Repeater1.DataSource = ds.Tables[0];
Repeater1.DataBind();
if (ds.Tables[0].Rows.Count > 0)
{
txt_not_found.Visible = false;
}
else
{
txt_not_found.Visible = true;
}
}
}

dont use quotes in first parameter:
cmd = new SqlCommand("Select * from UserProfile Where "+_var_search+"='" + _var_by + "'", con);
even better use paramaters:
cmd = new SqlCommand("Select * from UserProfile Where "+_var_search+"=#param", con);
cmd.Parameters.AddWithValue("#param", _var_by);

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();
}

Winform DataGridView still shows data after final update

I'm developing a winform application using C#.net, using DataGridView to display the data, but after the last update, the DataGridView returns 1 last row, but i want my DataGridView to return a blank row or just show the header only...
nb: Before the update JmlKirimK value is 1.
try
{
con.Close();
con.Open();
MySqlCommand cmd = new MySqlCommand("Select PasienID, NoHP, JmlKirimK from datapasien where JmlKirimK ='1' AND StatusDiabetes='Ya'", con);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
con.Close();
con.Open();
nohp = Convert.ToString(dr[1]);
idpasien = Convert.ToString(dr[0]);
jml = Convert.ToString(dr[2]);
Cursor.Current = Cursors.WaitCursor;
if (objclsSMS.sendMsg(this.port2, nohp, this.txtisi.Text))
{
MySqlCommand cmd3 = new MySqlCommand("UPDATE datapasien SET JmlKirimK='2' Where PasienID='" + idpasien.Trim() + "'", con);
cmd3.ExecuteNonQuery();
terkirim = terkirim + 1;
showgridkirimulang();
}
else
{
tdkterkiri = tdkterkiri + 1;
}
showgridkirimreset();
}
con.Close();
alert.ContentImage = SmsGatewayProlanis.Properties.Resources.Warning1;
alert.CaptionText = "Status";
alert.ContentText = "Pesan Terkirim = " + terkirim + ", Pesan Tidak Terkirim = " + tdkterkiri;
alert.Show();
}
catch (Exception ex)
{
alert.ContentImage = SmsGatewayProlanis.Properties.Resources.Warning1;
alert.CaptionText = "Status";
alert.ContentText = ex.Message;
alert.Show();
ErrorLog(ex.Message);
}
This is my DataGridView code
private void showgridkirimulang()
{
con.Close();
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT NamaPasien, RiwayatPenyakit, AlamatPasien, UmurPasien, Jeniskelamin, NoHP, JmlKirimK, StatusDiabetes FROM datapasien WHERE JmlKirimK=1 AND StatusDiabetes='Ya'", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.ExecuteNonQuery();
if (ds.Tables[0].Rows.Count > 0)
{
gridkirimulang.DataSource = ds.Tables[0];
gridkirimulang.Columns[0].HeaderText = "Nama Pasien";
gridkirimulang.Columns[1].HeaderText = "Riwayat Penyakit";
gridkirimulang.Columns[2].HeaderText = "Alamat Pasien";
gridkirimulang.Columns[3].HeaderText = "Umur Pasien";
gridkirimulang.Columns[4].HeaderText = "Jenis Kelamin";
gridkirimulang.Columns[5].HeaderText = "No. HP";
gridkirimulang.Columns[6].HeaderText = "Jumlah Pesan Terkirim";
gridkirimulang.Columns[7].HeaderText = "Status Diabetes";
gridkirimulang.MasterGridViewTemplate.BestFitColumns();
}
con.Close();
}

Showing alert panel when inventory reaches critical

I'm trying to show when an inventory is lower than critical. It doesn't seem to get there when I debug the system
This is my code behind
protected void Page_Load(object sender, EventArgs e)
{
GetInventory();
TotalCount();
CriticalItem();
Panel1.Visible = false;
}
void TotalCount()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT 'Total Count of Inventory ' + '(' + convert(nvarchar,SUM(Quantity)) + ')' AS TotalCount from Inventory";
SqlDataReader data = cmd.ExecuteReader();
if (data.HasRows)
{
while (data.Read())
{
lblTotalCount.Text = data["TotalCount"].ToString();
}
con.Close();
}
else
{
con.Close();
Response.Redirect("Default.aspx");
}
}
void GetInventory()
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT Inventory.InventoryID, Products.ProductName, " +
"Supplier.SupplierName, Inventory.Quantity, Users.LastName + ', ' + Users.FirstName AS UserAccount, " +
"SupplierProducts.CriticalLevel, Inventory.Status, Inventory.DateAdded, Inventory.DateModified, SupplierProducts.Price FROM Inventory " +
"INNER JOIN SupplierProducts ON Inventory.ProductID = SupplierProducts.SupplierProductID " +
"INNER JOIN Products ON SupplierProducts.ProductID = Products.ProductID " +
"INNER JOIN Supplier ON Inventory.SupplierID = Supplier.SupplierID " +
"INNER JOIN Users ON Inventory.UserID = Users.UserID";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Inventory");
lvInventory.DataSource = ds;
lvInventory.DataBind();
con.Close();
}
void CriticalItem()
{
var intcheck = new DataTable();
using (var da = new SqlDataAdapter("SELECT * FROM Inventory", con))
{
da.Fill(intcheck);
}
var critcheck = new DataTable();
using (var da = new SqlDataAdapter("SELECT * FROM SupplierProducts", con))
{
da.Fill(critcheck);
}
int finalQuantity = Convert.ToInt32(intcheck.Rows[0]["Quantity"]);
int criticalLevel = Convert.ToInt32(critcheck.Rows[0]["CriticalLevel"]);
if (finalQuantity > criticalLevel)
{
Panel1.Visible = true;
criticalItem.Text = "Available";
}
}
I want to criticalItem.text to tell the Inventory that is less than its critical level.

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 []

why it get old dataset

My program is working normally when I select a listbox item.it getting datas correct dataset and correctly.when I write to textbox,my source method working and finding right results.but after when I selected listbox item it bringing old dataset datas to screen.What can I do in this methods?
public void ara(string str, int neyegore)
{
sqlcon.Open();
if (neyegore == 0)
komut = new SqlCommand("select * from FilmTablo WHERE ad LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 1)
komut = new SqlCommand("select * from FilmTablo WHERE tur LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 2)
komut = new SqlCommand("select * from FilmTablo WHERE yonetmen LIKE '%" + str + "%'", sqlcon);
else if (neyegore == 3)
komut = new SqlCommand("select * from FilmTablo WHERE oyuncular LIKE '%" + str + "%'", sqlcon);
sdr = null;
sdr = komut.ExecuteReader();
dt2 = new DataTable();
dt2.Load(sdr);
ds2 = new DataSet();
ds2.Tables.Add(dt2);
listBox1.Items.Clear();
// sayac2 = 1;
int i = 0;
while (i < ds2.Tables[0].Rows.Count)
{
listBox1.Items.Add(ds2.Tables[0].Rows[i].ItemArray[0]);
i++;
}
sqlcon.Close();
}
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
txtad.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[0].ToString().Trim();
txttur.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[1].ToString().Trim();
txtsure.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[2].ToString().Trim();
txtyonetmen.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[3].ToString().Trim();
txtoyuncular.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[4].ToString().Trim();
txtsenaryo.Text = ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[5].ToString().Trim();
byte[] resim = (byte[])ds.Tables[1].Rows[listBox1.SelectedIndex].ItemArray[7];
image1.Source = GetBitmapImage(resim);
}
public void listeyiguncelle()
{
sqlcon.Open();
komut = new SqlCommand("select * from FilmTablo", sqlcon);
sdr = null;
sdr = komut.ExecuteReader();
dt = new DataTable();
dt.Load(sdr);
ds = new FilmDataDataSet();
ds.Tables.Add(dt);
listBox1.Items.Clear();
int i = 0;
//MessageBox.Show(ds.Tables[1].Rows.Count.ToString());
while (i < ds.Tables[1].Rows.Count)
{
listBox1.Items.Add(ds.Tables[1].Rows[i].ItemArray[0]);
i++;
}
sqlcon.Close();
lblfilmsayisi.Content = ds.Tables[1].Rows.Count;
}

Categories

Resources