I've a form in which I've data of Products of one store. i am accessing product Name and Price by just simply Providing Product code into other form, now here i want that when i open my Products form in which i have used list view to show already added products, i can easily click on any row , click edit button and the selected list view row becomes edit able , i simply update data in list view (by writing it in list view) and then click on save and it just automatically save data into my data base using update query..
the code through which i am loading products data in list view is :
private void frm4_Load(object sender, EventArgs e)
{
fnc_LoadProductsInfo(lvProducts);
}
private void fnc_LoadProductsInfo(ListView lv)
{
string sql;
sql = "";
sql += "SELECT * FROM ProductLog ORDER BY ItemNo";
SqlConnection con = new SqlConnection();
clsConnection clsCon = new clsConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader sdr;
clsCon.fnc_ConnectToDB(ref con);
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
sdr = cmd.ExecuteReader();
lv.Items.Clear();
ListViewItem lvItem;
while (sdr.Read())
{
lvItem = new ListViewItem();
lvItem.Text = Convert.ToString(sdr.GetValue(sdr.GetOrdinal("ItemNo")));
lvItem.SubItems.Add(Convert.ToString(sdr.GetValue(sdr.GetOrdinal("ProductCode"))));
lvItem.SubItems.Add(Convert.ToString(sdr.GetValue(sdr.GetOrdinal("ProductName"))));
lvItem.SubItems.Add(Convert.ToString(sdr.GetValue(sdr.GetOrdinal("ProductPrice"))));
// lvItem.SubItems.Add(Convert.ToString(sdr.GetValue(sdr.GetOrdinal("TotalPrice"))));
//lvItem.SubItems.Add(Convert.ToString(sdr.GetValue(sdr.GetOrdinal("Password"))));
lv.Items.Add(lvItem);
lvItem = null;
}
// lblTotalRecords.Text = Convert.ToString(lv.Items.Count);
sdr.Close();
sdr = null;
cmd = null;
con.Close();
con = null;
}
is it possible to make list view editable in it's own column and if yes then how can i do it?? any other suggestion will also be appreciable ...
Related
when i load my application i need to fill the combobox cbkeuze with the row loginnaam from table gebruik
The error I am getting is: Can't change the items because property Data Source is set.
Here is my code:
private void Form1_Load(object sender, EventArgs e)
{
// SQL Connectie opzetten
SqlConnection Conn = new SqlConnection();
Conn.ConnectionString = #"Integrated Security=true;Initial Catalog=Wachtwoord;Data Source=LAPTOP-PDI9B3LP\SCHOOL";
Conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = Conn;
// Alles selecteren van tabel Favorieten
cmd.CommandText = "select * from gebruik";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
// Tabel wegschrijven in Applicatie
string loginnaam = dr.GetString(0);
cbKeuze.Items.Add(loginnaam);
}
dr.Close();
// Database connectie sluiten
Conn.Close();
}
You should check your combobox properties and see if DataSource is initiated
You can add loginnaam's to List loginnaams until while loop ends. After dr.Close() line, use cbKeuze.DataSource = loginnaams.
This will work for you.
I want to display records in pivot grid (devexpress). The data will return from Stored Procedure
pivotTest is the name of pivotgrid,I can give the connection String later. But I don't how to bind data to pivot grid.
One more doubt. In which scenario,we should use SqlDataReader and SqlDataAdapter
private void ReportTestForm_Load(object sender, EventArgs e)
{
string cs = "";
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spProcedureTest", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
pivotTest.DataSource = rd;
pivotTest.DataBindings;
}
}
Why does my grid view not refresh with data in it? It adds to the database but then clears my grid view and has nothing in it. It loads the form and I can enter information into it hit the add button and it adds to database but grid view refresh puts no data into it.
private void addButton_Click(object sender, EventArgs e)
{
string title = titleTextBox.Text;
string starring = starringTextBox.Text;
string year = yearTextBox.Text;
SqlConnection conn = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=<PATH TO FILE>);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO dbo.Movie (Title, Starring, Year) VALUES (#title, #starring, #year)";
cmd.Parameters.AddWithValue("#Title", title);
cmd.Parameters.AddWithValue("#Starring", starring);
cmd.Parameters.AddWithValue("#Year", year);
cmd.Connection = conn;
cmd.ExecuteNonQuery();
SqlDataAdapter MyDataAdapter = new SqlDataAdapter(cmd.CommandText, conn);
titleTextBox.Text = "";
starringTextBox.Text = "";
yearTextBox.Text = "";
titleTextBox.Focus();
movieTableAdapter.Fill(moviesDataSet.Movie);
movieBindingSource.DataSource = movieTableAdapter;
myDataGridView.DataSource = movieBindingSource;
myDataGridView.Refresh();
myDataGridView.Update();
conn.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'moviesDataSet.Movie' table. You can move, or remove it, as needed.
this.movieTableAdapter.Fill(this.moviesDataSet.Movie);
}
SqlDataAdapter MyDataAdapter = new SqlDataAdapter("select query here", conn);
Pass select query to adapter. You are passing an insert query.
I’ve two comboboxes which should contain two different informations.
1.cb1: select table_name from information_schema.tables (this display multiple tables)
2.cb2: should populate it with a column name.
Example: I've three tables in cb1 with the same attributes but have different values at the column EmpName (tblLondon,tblBerlin,tblRom,...)
Now I wanna display in second comboboxe the column EmpName dynamically whenever I choose a table in first combobox.
cb1[tblLondon] cb2[John,Mavis,Chris,Mike..]
OR
cb1[tblBerlin] cb2[Günther,Peter, Sophie,Sunny, ..]
Can u plz help me out
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC");
try
{
// Open connection, Save the results in the DT and execute the spProc & fill it in the DT
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
cbTbl.DisplayMember = "TABLE_NAME";
cbTbl.ValueMember = "TABLE_NAME";
//Fill combobox with data in DT
cbTbl.DataSource = dt;
// Empty bzw. clear the combobox
cbTbl.SelectedIndex = -1;
This code is working and populating my cb1 (combobox)
And now i don't really know how to go about with cb2
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
if I understand you correctly, and if you have this data in SQL server or other database you should use SelectedIndexChange event and load item for that Id (use Display Member and Value Member for match Id).
take a look at this.it might help you
Edit :
you can do this with below code : (if you don't use database)
you should use this code in cb1.SelectedIndexChange (or value)
var cb2items = new Dictionary<int, string> {{1, "Name"}, {1, "anotherName"},{2,"Name"},{2, "anotherName"}}; // use the number for parent Id in cb1
foreach (var item in cb2items)
{
if (item.Key == int.Parse(comboBox1.SelectedValue.ToString()))
{
comboBox2.Items.Add(item);
}
}
Edit 2 :
use this code in cb1.SelectedValueChange:
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT ... WHERE TableName = cb1.SelectedValue");
spProc & fill it in the DT
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
cbTbl.DisplayMember = "TABLE_NAME";
cbTbl.ValueMember = "TABLE_NAME";
cbTb2.DataSource = dt;
and you also can create a procedure that send back the items from table name as input. if you use procedure, your code perform better.
Edit 3 :
put this code in cbTb2.SelectedValueChange event :
try
{
int a = int.Parse(cbTB2.SelectedValue.ToString());
}
catch { }
You might want to see combobox's events, "SelectedIndexChanged" or "SelectedValueChanged" should do it
I have a (Sql Server 2008) table called Courses with course_id,course_name and course_description as its fields
In the front end, I have a text box and a search button. In the text box, when I give a course name (even a part of it)..in the button click event, all the course names should show up.
How do I code this in C#? Any help would be appreciated..
you can select from sql table with where statement
eg, "whre course_name = 'a'"
a means it will return all course name with a character a
for eg, matehmatics
can search for details about * thing in google.
First of all, you should use "LIKE" operator in sql command in order to list all the results containing the criteria.
Secondly, you should use SqlDataReader given that you are only retrieving values.
Thirdly, you should use a parameter in order to prevent sql injection.
Since you did not specify how you want to display the results, I populate a list from the results in my sample code below. I hope this helps you and future viewers.
private void button1_Click(object sender, EventArgs e)
{
List<string> Courses = new List<string>();
SqlConnection con = new SqlConnection("the connection string here");
SqlDataReader reader;
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SELECT courseName, courseDescription FROM db.Courses WHERE CourseName LIKE %#CourseName%";
cmd.Parameters.AddWithValue("#CourseName", textBox1.Text);
con.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
string course = reader["CourseName"].ToString();
course += ", " + reader["CourseDescription"].ToString();
Courses.Add(course);
}
reader.Close();
foreach (string course in Courses)
{
//wherever and however you would like to display
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select Processor,HDD,RAM,Display,Graphics,OS,processor,hdd,ram,display,os,opticaldrive,warranty,price,other,graphics,images,Warranty,Price,Images,other from System where CompanyName='"+companyname.Text+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
processor.Text = dr.GetValue(3).ToString();
}
con.Close();
}