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;
Related
I have below data in SQL query, and am showing the current user "Domain" logged in I want to show the full name in label based on each user using the Windows form,
I got stuck here, how I can continue to find the value
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
label1.Text = userName.ToUpper();
SqlConnection con = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=db;User ID=AAA;password=******");
con.Open();
string command7 = "SELECT distinct [fullname] ,[Group] ,[Domain] FROM [tableA] where fullname is not null and [group] is not null and [Domian] = '"+ label1.Text + "'";
SqlCommand da7 = new SqlCommand(command7, con);
Full name
Group
Domain
Alex Sam J
A GROUP
test\Alex
Jon Pete F
B GROUP
test\Jon
You can get the value of full name this way :
...
SqlCommand cmd = new SqlCommand(command7, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
labelFullName.Text = da.Tables[0].Rows[0]["fullname"].ToString();
I solve it like this, thanks
SqlCommand cmd = new SqlCommand(da7, con);
cmd.CommandType = CommandType.Text;
//con.Open();
SqlDataReader reader = cmd.ExecuteReader();
//DataTable dt7 = new DataTable();
while (reader.Read())
{
label21.Text = reader["Technician"].ToString();
}
con.Close();
I have a winform project with an access file as a database.
In one of my forms I want the user to choose a company from a combobox.
(the list of campanies is in the access table)
How do I set the company name column to be combobox dropdown values ?
Try this
string qr1 = "select companyname from table1";
SqlCommand cmd1 = new SqlCommand(qr1, con);
con.Open();
SqlDataReader dr1 = cmd1.ExecuteReader();
cmbcat.Items.Clear();
while (dr1.Read())
{
cmbcat.Items.Add(dr1[0].ToString());
}
con.Close();
to Nimi
is this oledbcommand?
my solutions is ....
public void make_cbDispatch()
{
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;"
+ "Data Source=I:\\Projects\\project.accdb;"
+ "Persist Security Info=False;";
string qr1 = "SELECT DISTINCT object "
+ "FROM tList "
+ "ORDER BY object ";
OleDbConnection con = new OleDbConnection(ConnectionString);
OleDbCommand cmd1 = new OleDbCommand(qr1, con);
con.Open();
OleDbDataReader dr1 = cmd1.ExecuteReader();
cbDispatch.Items.Clear();
while (dr1.Read())
{
cbDispatch.Items.Add(dr1[0].ToString());
}
con.Close();
}
today, I am trying to use DataRoutines to insert some values into my Access Database. However, it did not work as well as there were no records created. Does anyone know what's wrong with my code?
This is my DataRoutines (dataroutines2) file
public void CreateRecs(string strUserId)
{
OleDbConnection mDB = new OleDbConnection();
mDB.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data source="
+ Server.MapPath("~/App_Data/webBase.accdb");
OleDbCommand cmd; OleDbDataReader rdr;
string strSql;
string strOFlag = "F";
// check to see if there is an active order record
strSql = "SELECT eStatus FROM enrolInfo WHERE eUserId = #UserId "
+ "ORDER BY eEnrolNo DESC;";
cmd = new OleDbCommand(strSql, mDB);
cmd.Parameters.Add("#UserId", OleDbType.Char).Value = strUserId;
mDB.Open();
rdr = cmd.ExecuteReader();
Boolean booRows = rdr.HasRows;
if (booRows) //when booRows is true, there are order records for the user
{
rdr.Read();
if ((string)rdr["eStatus"] != "Ordering") //status of an active order is "Ordering"
{
strOFlag = "M"; //"T" means there is a need to create a new Orders record
}
else { strOFlag = "M"; }
mDB.Close();
if (strOFlag == "M")
{
//insert a new order record
string strStatus = "Ordering";
strSql = "INSERT INTO enrolInfo (eUserId, eStatus) VALUES (#UserId, #Status)";
cmd = new OleDbCommand(strSql, mDB);
cmd.Parameters.AddWithValue("#UserId", strUserId);
cmd.Parameters.AddWithValue("#Status", strStatus);
mDB.Open();
cmd.ExecuteNonQuery();
mDB.Close();
}
//get back order No - this order no is needed when the user buys an item
strSql = "SELECT eEnrolNo FROM enrolInfo WHERE eUserId = #UserId "
+ "ORDER BY eEnrolNo DESC;";
cmd = new OleDbCommand(strSql, mDB);
cmd.Parameters.Add("#UserId", OleDbType.Char).Value = strUserId;
mDB.Open();
rdr = cmd.ExecuteReader();
rdr.Read();
Session["tOrderNo"] = rdr["eEnrolNo"]; //store the active order no in the sOrderNo
mDB.Close();
return;
}
}
This is the code that will route the solution to the DataRoutines file in my Register Page
//prepare Session variables for newly register customer
Session["sFlag"] = "M";
Session["tUserId"] = (string)txtUserId.Text;
Session["tName"] = (string)txtName.Text;
Session["tAddress"] = (string)txtAddress.Text;
Session["tTel"] = (string)txtTel.Text;
Session["tEmail"] = (string)txtEmail.Text;
String strUserId = (string)Session["tUserId"];
DataRoutines2 DRObject = new DataRoutines2();
DRObject.CreateRecs(strUserId);
Thanks for your help
hi im using a dropdownlist to get id from a table and then delete the line with a delete buton when i run the page teh dropdownlist get all the id's in the table all fine but when i delete it always delete the last id even if i select another
List<classe_cv_langues> li6 = new List<classe_cv_langues>();
SqlConnection con4 = new SqlConnection(#"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
SqlCommand cmd4 = new SqlCommand();
cmd4.Connection = con4;
con4.Open();
cmd4.CommandText = "select id from cv_langues as cl inner join cv as c on cl.id_cv = c.id_cv where id_candidat= " + Session["Id_candidat"];
SqlDataReader dr4 = cmd4.ExecuteReader();
while (dr4.Read())
{
classe_cv_langues p6 = new classe_cv_langues();
p6.Id = int.Parse(dr4[0].ToString());
li6.Add(p6);
}
dr4.Close();
con4.Close();
DropDownList7.DataSource = li6;
DropDownList7.DataTextField = "id";
DropDownList7.DataValueField ="id";
DropDownList7.DataBind()
the delete buton:
SqlConnection con = new SqlConnection(#"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "delete from cv_langues where id='"+DropDownList7.SelectedValue+"'";
cmd.ExecuteNonQuery();
con.Close();
Server.Transfer("gestion_cv.aspx");
Assuming your binding logic is on page_load method. Try testing for Page.IsPostBack
if (!Page.IsPostBack)
{
List<classe_cv_langues> li6 = new List<classe_cv_langues>();
SqlConnection con4 = new SqlConnection(#"Data Source=p5-pc\sqlexpress;Initial Catalog=recrutement_online_3;Integrated Security=True");
SqlCommand cmd4 = new SqlCommand();
cmd4.Connection = con4;
con4.Open();
cmd4.CommandText = "select id from cv_langues as cl inner join cv as c on cl.id_cv = c.id_cv where id_candidat= " + Session["Id_candidat"];
SqlDataReader dr4 = cmd4.ExecuteReader();
while (dr4.Read())
{
classe_cv_langues p6 = new classe_cv_langues();
p6.Id = int.Parse(dr4[0].ToString());
li6.Add(p6);
}
dr4.Close();
con4.Close();
DropDownList7.DataSource = li6;
DropDownList7.DataTextField = "id";
DropDownList7.DataValueField ="id";
DropDownList7.DataBind()
}
How to filter data in datagrid for example if you select the combo box in student number then input 1001 in the text field. All records in 1001 will appear in datagrid. I am using sql server
private void button2_Click(object sender, EventArgs e)
{
if (cbofilter.SelectedIndex == 0)
{
string sql;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server= " + Environment.MachineName.ToString() + #"\; Initial Catalog=TEST;Integrated Security = true";
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds1 = new DataSet();
ds1 = DBConn.getStudentDetails("sp_RetrieveSTUDNO");
sql = "Select * from Test where STUDNO like '" + txtvalue.Text + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.CommandType = CommandType.Text;
da.SelectCommand = cmd;
da.Fill(ds1);
dbgStudentDetails.DataSource = ds1;
dbgStudentDetails.DataMember = ds1.Tables[0].TableName;
dbgStudentDetails.Refresh();
}
else if (cbofilter.SelectedIndex == 1)
{
//string sql;
//SqlConnection conn = new SqlConnection();
//conn.ConnectionString = "Server= " + Environment.MachineName.ToString() + #"\; Initial Catalog=TEST;Integrated Security = true";
//SqlDataAdapter da = new SqlDataAdapter();
//DataSet ds1 = new DataSet();
//ds1 = DBConn.getStudentDetails("sp_RetrieveSTUDNO");
//sql = "Select * from Test where Name like '" + txtvalue.Text + "'";
//SqlCommand cmd = new SqlCommand(sql,conn);
//cmd.CommandType = CommandType.Text;
//da.SelectCommand = cmd;
//da.Fill(ds1);
// dbgStudentDetails.DataSource = ds1;
//dbgStudentDetails.DataMember = ds1.Tables[0].TableName;
//ds.Tables[0].DefaultView.RowFilter = "Studno = + txtvalue.text + ";
dbgStudentDetails.DataSource = ds.Tables[0];
dbgStudentDetails.Refresh();
}
}
It's difficult to answer pricisely to a vague question. I guess that you'll have to adapt your SQL query with a WHERE statement containing the user input.
If 'student number' is selected in the combo box, query like this (numbers starting with):
SELECT id, name, number FROM students WHERE number LIKE #search + '%'
If 'student name' is selected, use another query (names containing):
SELECT id, name, number FROM students WHERE name LIKE '%' + #search + '%'
Please explain in what sense C# is concerned.
You don't say what is wrong with the code you commented out. You also don't say what type the Studno column is.
Have you tried something like:
ds1.Tables[0].DefaultView.RowFilter = "Studno = '" + txtvalue.text + "'";