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 []
Related
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
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);
I have table consisting columns(....,....,....,BLOCK) in my database.
The BLOCK column has bit datatype(True,False).
When BLOCK column has False, the data should be fetched from the database.
When BLOCK column has True, the data should not be fetched resulting in throwing an error.
When I give the name of a particular person in textbox and click button, the above operation must be performed
my button click c# coding is...
protected void ImageButton5_Click(object sender, ImageClickEventArgs e)
{
string selectsql = "SELECT * FROM UserDetailsTwo";
using (SqlConnection con = new SqlConnection(#"Data Source=ENTERKEY001;Initial Catalog=ContactManagement;Integrated Security=True"))//DataBase Connection
{
SqlCommand selectCommand = new SqlCommand(selectsql, con);
con.Open();
SqlDataReader SelectReader = selectCommand.ExecuteReader();
while (SelectReader.Read())
{
Boolean BLOCK = Convert.ToBoolean(SelectReader["BLOCK"]);
if (BLOCK == false)
{
//con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SearchUser";
cmd.Parameters.AddWithValue("#NAME", TextBox4.Text.Trim());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
else
{
Response.Write("Error");
}
}
SelectReader.Close();
}
}
You are trying to use an SQLDataReader like a DataAdapter.
SelectReader = selectCommand.ExecuteReader();
while (SelectReader.Read())
{
Int64 BLOCK = Convert.ToInt64(SelectReader["BLOCK"]);
if (BLOCK == false)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SearchUser";
cmd.Parameters.AddWithValue("#NAME", TextBox4.Text.Trim());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataBind();
con.Close();
}
else
{
Response.Write("Error");
}
}
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader%28v=vs.110%29.aspx
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();
}
private void button1_Click(object sender, EventArgs e)
{
Insert();
}
private int Insert()
{
string Query = "insert into person values ('" + textBox4.Text + "','" + textBox5.Text + "')";
string connectionString = #"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=prac;Integrated Security=True";
SqlConnection cn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(Query, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
DataTable dt = new DataTable();
da.Fill(dt);
int RowEffected = cmd.ExecuteNonQuery();
cn.Dispose();
if (RowEffected > 0)
{
MessageBox.Show("Record Affected");
}
GetDBConnection();
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
return 0;
}
public Form1()
{
InitializeComponent();
GetDBConnection();
//Insert();
}
private void GetDBConnection()
{
string connnectionstring = #"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=prac;Integrated Security=True";
SqlDataAdapter da = new SqlDataAdapter("select * from person", connnectionstring);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
comboBox1.DataSource = dt;
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "lname";
}
When you do
da.Fill(dt);
cmd passed to da in it's constructor is executed. That's first time.
Second is in ExecuteNonQuery.
Cmd in sqlDataAdapter should be SELECT command.