I have this 2 Datagridviews, dgv1 and 2. How can i check if dgv2 has no "content" or rows?
For example i want to do: Send OrderID: 0001(from dgv1) to archives if dgv 2 is "empty" or no rows? basically i want to remove this Order is its dgv2 has no rows or no products left.
dgv2's content is related to dgv1's primary key btw.
private void dgvReceiving_CellClick(object sender, DataGridViewCellEventArgs e)
{
using (SqlConnection connection = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True"))
{
SqlCommand command =
new SqlCommand("select OrderID,SupplierName,LeadTime,OrderedBy,DateOrdered,Status,DateToReceived from Orders where OrderID = '" + dgvReceiving.CurrentRow.Cells[0].Value.ToString() + "'", connection);
connection.Open();
SqlDataReader read = command.ExecuteReader();
while (read.Read())
{
rorderid.Text = (read["OrderID"].ToString());
rsupplier.Text = (read["SupplierName"].ToString());
rleadtime.Text = (read["LeadTime"].ToString());
rordered.Text = (read["OrderedBy"].ToString());
rdateordered.Text = (read["DateOrdered"].ToString());
rdatedelivery.Text = (read["DateToReceived"].ToString());
rstatus.Text = (read["Status"].ToString());
}
SqlConnection cn2 = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True");
cn2.Open();
string amt = "select sum(TotalPrice) from Orders_productholder where OrderID = '" + rorderid.Text + "'";
SqlCommand cmd2 = new SqlCommand(amt, cn2);
labelsupertotal.Text = "P "+cmd2.ExecuteScalar().ToString();
}
dgvreceivingproduct();
}
private void dgvreceivingproduct()
{
SqlConnection cn3 = new SqlConnection("Data Source=DESKTOP-MQKIBSK\\SQLEXPRESS;Initial Catalog=MARISCHELLdatabase;Integrated Security=True");
cn3.Open();
string qry = "Select Status,ID,ProductID,ProductName,Dosage,Price,QtyOrdered,TotalPrice,ExpirationDate,SellingPrice,BatchNumber from Orders_productholder where Status = 'Unreceived' and OrderID = '" + dgvReceiving.CurrentRow.Cells[0].Value.ToString() + "' ";
SqlCommand cmd3 = new SqlCommand(qry, cn3);
DataTable poholder = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd3);
adapter.Fill(poholder);
dgvReceivingproducts.DataSource = poholder;
}
use the property (dgv2.RowCount > 0)
I am trying to copy specified data from table "sale" to another table "sale_temp", which is specified by "where inv_reff = #inv_reff". I am working on visual studio 2010 C# winform.
My code do nothing till i remove "where inv_reff = #inv_reff & its parameter".
my code
private void btn_srch_sale_Click(object sender, EventArgs e)
{
int s;
DataTable dt = new DataTable();
SqlDataAdapter dat = new SqlDataAdapter();
SqlConnection con = new SqlConnection("Data Source=Pakrelible\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
//SqlCommand dat = new SqlCommand();
con.Open();
dat.SelectCommand = new SqlCommand("SELECT * FROM sale_temp", con);
dat.SelectCommand.Connection = con;
s = dat.Fill(dt);
if (s >= 1)
{
System.Media.SystemSounds.Hand.Play();
MessageBox.Show("Please Complete Sale Invoice First...!!!", "Error");
this.aca.Focus();
return;
}
int x;
SqlConnection conn = new SqlConnection("Data Source=Pakrelible\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
***SqlCommand copy = new SqlCommand("SELECT * FROM sale where inv_reff = #inv_reff "); \\ problem goes here***
copy.Parameters.Add("#inv_reff", SqlDbType.VarChar).Value = inv_sale.Text;
conn.Open();
copy.Connection = conn;
SqlDataReader sale_copy = copy.ExecuteReader();
sale_copy.Read();
while (sale_copy.Read())
{
aca_sale.Text = sale_copy["aca"].ToString();
acn_sale.Text = sale_copy["acn"].ToString();
act_sale.Text = sale_copy["act"].ToString();
tele_sale.Text = sale_copy["tele"].ToString();
memo_sale.Text = sale_copy["memo"].ToString();
memo2_sale.Text = sale_copy["memo2"].ToString();
inv_sale.Text = sale_copy["inv_reff"].ToString();
inv_date_sale.Text = sale_copy["inv_date"].ToString();
mode_sale.Text = sale_copy["mode"].ToString();
reff_sale.Text = sale_copy["reff"].ToString();
employe_sale.Text = sale_copy["employe"].ToString();
dis_sale.Text = sale_copy["dis"].ToString();
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection connn = new SqlConnection("Data Source=Pakrelible\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
da.InsertCommand = new SqlCommand("insert into sale_temp (aca,acn,act,tele,memo,memo2,inv_reff,inv_date,mode,reff,brand,part_no,descrp,qty,unt_prc,total_prc,employe,dis )" + "values(#aca,#acn,#act,#tele,#memo,#memo2,#inv_reff,#inv_date,#mode,#reff,#brand,#part_no,#descrp,#qty,#unt_prc,#total_prc,#employe,#dis )", connn);
da.InsertCommand.Parameters.Add("#aca", SqlDbType.VarChar).Value = sale_copy["aca"].ToString();
da.InsertCommand.Parameters.Add("#acn", SqlDbType.VarChar).Value = sale_copy["acn"].ToString();
da.InsertCommand.Parameters.Add("#act", SqlDbType.VarChar).Value = sale_copy["act"].ToString();
da.InsertCommand.Parameters.Add("#tele", SqlDbType.VarChar).Value = sale_copy["tele"].ToString();
da.InsertCommand.Parameters.Add("#memo", SqlDbType.VarChar).Value = sale_copy["memo"].ToString();
da.InsertCommand.Parameters.Add("#memo2", SqlDbType.VarChar).Value = sale_copy["memo2"].ToString();
da.InsertCommand.Parameters.Add("#inv_reff", SqlDbType.VarChar).Value = sale_copy["inv_reff"].ToString();
da.InsertCommand.Parameters.Add("#inv_date", SqlDbType.VarChar).Value = sale_copy["inv_date"].ToString();
da.InsertCommand.Parameters.Add("#mode", SqlDbType.VarChar).Value = sale_copy["mode"].ToString();
da.InsertCommand.Parameters.Add("#reff", SqlDbType.VarChar).Value = sale_copy["reff"].ToString();
da.InsertCommand.Parameters.Add("#brand", SqlDbType.VarChar).Value = sale_copy["brand"].ToString();
//da.InsertCommand.Parameters.Add("#brand", SqlDbType.VarChar).Value = sale_copy["brand"].ToString();
da.InsertCommand.Parameters.Add("#part_no", SqlDbType.VarChar).Value = sale_copy["part_no"].ToString();
da.InsertCommand.Parameters.Add("#descrp", SqlDbType.VarChar).Value = sale_copy["descrp"].ToString();
da.InsertCommand.Parameters.Add("#qty", SqlDbType.VarChar).Value = sale_copy["qty"].ToString();
da.InsertCommand.Parameters.Add("#unt_prc", SqlDbType.VarChar).Value = sale_copy["unt_prc"].ToString();
da.InsertCommand.Parameters.Add("#total_prc", SqlDbType.VarChar).Value = sale_copy["total_prc"].ToString();
da.InsertCommand.Parameters.Add("#employe", SqlDbType.VarChar).Value = sale_copy["employe"].ToString();
da.InsertCommand.Parameters.Add("#dis", SqlDbType.VarChar).Value = sale_copy["dis"].ToString();
//da.InsertCommand.Parameters.Add("#amnt_wrd", SqlDbType.VarChar).Value = sale_copy["amnt_wrd"].ToString();
connn.Open();
x = da.InsertCommand.ExecuteNonQuery();
if (x >= 1)
{
System.Media.SystemSounds.Asterisk.Play();
SqlConnection connnn = new SqlConnection("Data Source=Pakrelible\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
DataTable sdt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("SELECT brand,part_no,descrp,qty,unt_prc,total_prc FROM sale_temp ", connnn);
sda.Fill(sdt);
sale_grid.DataSource = sdt;
connnn.Dispose();
}
else
{
MessageBox.Show("Error Posting !", "Error");
}
}
if i remove "where inv_reff = #inv_reff" then thees codes copy complete data from table.
can any guy help me, may be i am missing something or have another way ...!! Please help me through coding or told my mistake,Don't give me links..
i found the problem and that is
"while (sale_copy.Read())" insteed of this only "(sale_copy.Read())"
Complete Codes
private void btn_srch_sale_Click(object sender, EventArgs e)
{
int s;
DataTable dt = new DataTable();
SqlDataAdapter dat = new SqlDataAdapter();
SqlConnection con = new SqlConnection("Data Source=Pakrelible\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
//SqlCommand dat = new SqlCommand();
con.Open();
dat.SelectCommand = new SqlCommand("SELECT * FROM sale_temp", con);
dat.SelectCommand.Connection = con;
s = dat.Fill(dt);
if (s >= 1)
{
System.Media.SystemSounds.Hand.Play();
MessageBox.Show("Please Complete Sale Invoice First...!!!", "Error");
this.aca.Focus();
return;
}
int x;
SqlConnection conn = new SqlConnection("Data Source=Pakrelible\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
***SqlCommand copy = new SqlCommand("SELECT * FROM sale where inv_reff = #inv_reff "); \\ problem goes here***
copy.Parameters.Add("#inv_reff", SqlDbType.VarChar).Value = inv_sale.Text;
conn.Open();
copy.Connection = conn;
SqlDataReader sale_copy = copy.ExecuteReader();
while (sale_copy.Read())
{
aca_sale.Text = sale_copy["aca"].ToString();
acn_sale.Text = sale_copy["acn"].ToString();
act_sale.Text = sale_copy["act"].ToString();
tele_sale.Text = sale_copy["tele"].ToString();
memo_sale.Text = sale_copy["memo"].ToString();
memo2_sale.Text = sale_copy["memo2"].ToString();
inv_sale.Text = sale_copy["inv_reff"].ToString();
inv_date_sale.Text = sale_copy["inv_date"].ToString();
mode_sale.Text = sale_copy["mode"].ToString();
reff_sale.Text = sale_copy["reff"].ToString();
employe_sale.Text = sale_copy["employe"].ToString();
dis_sale.Text = sale_copy["dis"].ToString();
SqlDataAdapter da = new SqlDataAdapter();
SqlConnection connn = new SqlConnection("Data Source=Pakrelible\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
da.InsertCommand = new SqlCommand("insert into sale_temp (aca,acn,act,tele,memo,memo2,inv_reff,inv_date,mode,reff,brand,part_no,descrp,qty,unt_prc,total_prc,employe,dis )" + "values(#aca,#acn,#act,#tele,#memo,#memo2,#inv_reff,#inv_date,#mode,#reff,#brand,#part_no,#descrp,#qty,#unt_prc,#total_prc,#employe,#dis )", connn);
da.InsertCommand.Parameters.Add("#aca", SqlDbType.VarChar).Value = sale_copy["aca"].ToString();
da.InsertCommand.Parameters.Add("#acn", SqlDbType.VarChar).Value = sale_copy["acn"].ToString();
da.InsertCommand.Parameters.Add("#act", SqlDbType.VarChar).Value = sale_copy["act"].ToString();
da.InsertCommand.Parameters.Add("#tele", SqlDbType.VarChar).Value = sale_copy["tele"].ToString();
da.InsertCommand.Parameters.Add("#memo", SqlDbType.VarChar).Value = sale_copy["memo"].ToString();
da.InsertCommand.Parameters.Add("#memo2", SqlDbType.VarChar).Value = sale_copy["memo2"].ToString();
da.InsertCommand.Parameters.Add("#inv_reff", SqlDbType.VarChar).Value = sale_copy["inv_reff"].ToString();
da.InsertCommand.Parameters.Add("#inv_date", SqlDbType.VarChar).Value = sale_copy["inv_date"].ToString();
da.InsertCommand.Parameters.Add("#mode", SqlDbType.VarChar).Value = sale_copy["mode"].ToString();
da.InsertCommand.Parameters.Add("#reff", SqlDbType.VarChar).Value = sale_copy["reff"].ToString();
da.InsertCommand.Parameters.Add("#brand", SqlDbType.VarChar).Value = sale_copy["brand"].ToString();
//da.InsertCommand.Parameters.Add("#brand", SqlDbType.VarChar).Value = sale_copy["brand"].ToString();
da.InsertCommand.Parameters.Add("#part_no", SqlDbType.VarChar).Value = sale_copy["part_no"].ToString();
da.InsertCommand.Parameters.Add("#descrp", SqlDbType.VarChar).Value = sale_copy["descrp"].ToString();
da.InsertCommand.Parameters.Add("#qty", SqlDbType.VarChar).Value = sale_copy["qty"].ToString();
da.InsertCommand.Parameters.Add("#unt_prc", SqlDbType.VarChar).Value = sale_copy["unt_prc"].ToString();
da.InsertCommand.Parameters.Add("#total_prc", SqlDbType.VarChar).Value = sale_copy["total_prc"].ToString();
da.InsertCommand.Parameters.Add("#employe", SqlDbType.VarChar).Value = sale_copy["employe"].ToString();
da.InsertCommand.Parameters.Add("#dis", SqlDbType.VarChar).Value = sale_copy["dis"].ToString();
//da.InsertCommand.Parameters.Add("#amnt_wrd", SqlDbType.VarChar).Value = sale_copy["amnt_wrd"].ToString();
connn.Open();
x = da.InsertCommand.ExecuteNonQuery();
if (x >= 1)
{
System.Media.SystemSounds.Asterisk.Play();
SqlConnection connnn = new SqlConnection("Data Source=Pakrelible\\SQLEXPRESS;AttachDbFilename=D:\\fuda\\Fuda.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
DataTable sdt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("SELECT brand,part_no,descrp,qty,unt_prc,total_prc FROM sale_temp ", connnn);
sda.Fill(sdt);
sale_grid.DataSource = sdt;
connnn.Dispose();
}
else
{
MessageBox.Show("Error Posting !", "Error");
}
}
I'm having problem with my sql query.
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLExpress;" + "Trusted_Connection=True;" + "User Instance=True;" + "AttachDbFilename=|DataDirectory|\\fbi.mdf;";
string sqlQuery4 = "SELECT Car FROM tbl1 JOIN tbl2 ON (tbl1.userID = tbl2.userID) WHERE tbl2.username='Bob'";
SqlCommand cmd4 = new SqlCommand(sqlQuery4, conn);
conn.Open();
SqlDataReader rd = cmd4.ExecuteReader();
rd.Read();
ddl1.Items.Add(rd.GetValue(0).ToString());
conn.Close();
So it should return all cars from tbl1 that belongs to Bob. Query only return one string and put it into Listbox "ddl1" while it should return at least 3 of them.
Any ideas?
You need to loop through the reader:
while (rd.Read()){
ddl1.Items.Add(rd.GetValue(0).ToString());
}
Novak, not sure what your issue was with Curt's solution, as it is correct. Your complete statement should look like:
string connectionString= "Data Source=.\\SQLExpress;Trusted_Connection=True;User Instance=True;AttachDbFilename=|DataDirectory|\\fbi.mdf;";
string query = "SELECT Car FROM tbl1 JOIN tbl2 ON (tbl1.userID = tbl2.userID) WHERE tbl2.username='Bob'";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
ddl1.Items.Add(reader.GetValue(0).ToString());
}
}
}
}
try below code:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLExpress;" + "Trusted_Connection=True;" + "User Instance=True;" + "AttachDbFilename=|DataDirectory|\\fbi.mdf;";
string sqlQuery4 = "SELECT Car FROM tbl1 JOIN tbl2 ON (tbl1.userID = tbl2.userID) WHERE tbl2.username='Bob'";
SqlCommand cmd4 = new SqlCommand(sqlQuery4, conn);
conn.Open();
SqlDataReader rd = cmd4.ExecuteReader();
ddl1.DataSource = rd;
ddl1..DataTextField = "columnname"; //your column name
ddl1.DataValueField = "columnname";
ddl1.DataBind();
rd.Close();
conn.Close();
I do have a ListView which i call from Database ( its selection of data ) in each row i have one Remove, Update Button.
i Bind each button to the ID in listview so when i click it have to return that row value only!
BUT i want to call this value in my query Function to update or Remove the selected Row.
I'm not sure how to call this value into Query.
here is my binding sample:
<Button Name="delete" Click="Delete_Click" CommandParameter="{Binding Path=empID}">
Here is how i call it :
Button _button = (Button)sender;
string empID = _button.CommandParameter.ToString();
\sql stuff
SqlCeConnection con = new SqlCeConnection();
SqlCeDataAdapter ad = new SqlCeDataAdapter();
SqlCeCommand cmd = new SqlCeCommand(empID);
String str = "UPDATE employee SET Isdeleted ='1' WHERE empID= " + empID;
cmd.Parameters.AddWithValue("#empID", empID);
ad.SelectCommand = cmd;
Hope i can get some help as this Query will never worked so far!
Not sure if it will work but u should give it a try, I made it for a local sdf database
var con = new SqlCeConnection("Data Source=" + "|DataDirectory|\\Database1.sdf");
String str = "UPDATE employee SET Isdeleted ='1' WHERE empID= '"+ empID +"'";
var cmd = new SqlCeCommand(str,con);
try
{
con.Open();
}
catch (Exception a)
{
Console.WriteLine(a.ToString());
}
finally
{
cmd.ExecuteNonQuery();
con.Close();
}
SqlCeConnection con = new SqlCeConnection();
SqlCeDataAdapter ad = new SqlCeDataAdapter();
SqlCeCommand cmd = new SqlCeCommand(empID);
String str = "UPDATE employee SET Isdeleted ='1' WHERE empID= " + empID;
cmd.Parameters.AddWithValue("#empID", empID);
ad.SelectCommand = cmd;
this is a strange setup
perhaps:
SqlCeConnection con = new SqlCeConnection();
SqlCeDataAdapter ad = new SqlCeDataAdapter();
String str = "UPDATE employee SET Isdeleted ='1' WHERE empID= #empID";
SqlCeCommand cmd = new SqlCeCommand(str);
cmd.Parameters.AddWithValue("#empID", empID);
ad.UpdateCommand = cmd;
I am new to C# and trying to populate a DropDownList based on a database value. I tried connecting to database as shown below - tested with the statement and it says connected. Can I assume this is correct? Am I on the right track? Also, how do I then select value from the table and fill DropDownList with a field?
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection (
"Data Source=.\\SQLEXPRESS;AttachDbFilename=C:customers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
try
{
connection.Open();
TextBox1.Text = "connected";
}
catch (Exception)
{
TextBox1.Text = " not connected";
}
}
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection (
"Data Source=.\\SQLEXPRESS;AttachDbFilename=C:customers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
try
{
SqlDataReader dReader;
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = CommandType.Text;
cmd.CommandText ="Select distinct [Name] from [Names]" +
" order by [Name] asc";
connection.Open();
dReader = cmd.ExecuteReader();
if (dReader.HasRows == true)
{
while (dReader.Read())
//Names collection is a combo box.
namesCollection.Add(dReader["Name"].ToString());
}
else
{
MessageBox.Show("Data not found");
}
dReader.Close()
TextBox1.Text = "connected";
}
catch (Exception)
{
TextBox1.Text = " not connected";
}
}
Hope that helps................
It's So Much Simple :----
SqlConnection con = new SqlConnection();
DataSet ds = new DataSet();
con.ConnectionString = #"Data Source=TOP7\SQLEXPRESS;Initial Catalog=t1;Persist Security Info=True;User ID=Test;Password=t123";
string query = "Select * from tbl_User";
SqlCommand cmd = new SqlCommand(query, con);
cmd.CommandText = query;
con.Open();
SqlDataAdapter adpt = new SqlDataAdapter(cmd);
adpt.Fill(ds);
comboBox1.Items.Clear();
comboBox1.DisplayMember = "UserName";
comboBox1.ValueMember = "UserId";
comboBox1.DataSource = ds.Tables[0];
------------------------------------------------------------------------
using (SqlConnection con = new SqlConnection("Data Source = NIPOON; Initial Catalog = CustomerOrders; Integrated Security = true"))
{
SqlCommand cmd = new SqlCommand("SELECT Name FROM Customer", con);
con.Open();
dropDownList.DataSource = cmd.ExecuteReader();
dropDownList.DataTextField = "Name";
dropDownList.DataValueField = "Name";
dropDownList.DataBind();
}