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.
Related
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 am trying to populate a listbox in C# from a table in sql server. The table is set up as:
tblTables
TableID int
SeatingCapacity int
CurrentCapacity int
The code I am trying to use is:
private void fillListBox()
{
String connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter();
String commandString = "SELECT TableID, SeatingCapacity, CurrentCapacity from tblTables";
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
conn.Open();
cmd.CommandText = commandString;
cmd.Connection = conn;
dr = cmd.ExecuteReader();
lstTableList.Items.Clear();
lstTableList.BeginUpdate();
while (dr.Read())
{
lstTableList.Items.Add(dr.GetInt32(0) + " - " + dr.GetInt32(1) + " - " + dr.GetInt32(2));
}
lstTableList.EndUpdate();
dr.Close();
conn.Close();
}
And the code for the parent form to transfer is:
private void mnuFormsTables_Click(object sender, EventArgs e)
{
frmTables aTables = new frmTables();
aTables.MdiParent = this;
aTables.Show();
aTables.Focus();
}
I call fillListBox() is the form load of frmTables. However when I put a break point on
String connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
It shows that right after that line the code jumps to the parent form at
aTables.Show();
Any help would be greatly appreciated. I am also not sure if that is the proper way to populate the listbox either. Preferably it would be nice if the listbox could show the table ID and add "Full" beside it if the current capacity >= seating capacity.
The github repo URL for this is https://github.com/resistince/StaufferRestaurant
EDIT: If I use:
String connectionString = Properties.Settings.Default.StaufferRestaurantConnectionString;
instead of using the ConfigurationManager it works. I have System.Configuration.dll as a reference and the proper using statement so I don't know why it doesn't work right.
I have two tables in a SQL Server database. I select from table ADMS and I need to insert master table by gridview but I dont know how to insert with gridview. Please help. I've tried for many days and I did not pass yet
protected void Button3_Click1(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
SqlConnection con = new SqlConnection(MyConnectionString);
// con.Open(); // don't need the Open, the Fill will open and close the connection automatically
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM ADMS_Machining where datetime='" + TextBox1.Text + "'", con);
mytable = new DataTable();
da.Fill(mytable);
GridView2.DataSource = mytable;
GridView2.DataBind();
}
else
{
SqlConnection con = new SqlConnection(MyConnectionString);
// con.Open(); // don't need the Open, the Fill will open and close the connection automatically
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Machining_Master where datetime='" + TextBox1.Text + "'", con);
mytable = new DataTable();
da.Fill(mytable);
GridView2.DataSource = mytable;
GridView2.DataBind();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
String strConnString, strSQL;
strConnString = "Server=kane-pc;UID=sa;PASSWORD=1234;Database=Machining;Max Pool Size=400;Connect Timeout=600;";
//here
conn.ConnectionString = conn;
conn.Open();
cmd.Connection = conn;
cmd.CommandText = strSQL;
}
You can extract values from a grid view depending on what you have placed in the cells...
string value = this.GridView2.Rows[0].Cells[0].Text;
You can also track the selected row event, and get specific controls like the following...
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
string someValueTakenFromLabel = (GridView2.SelectedRow.FindControl("lblAnyLabelHere") as Label).Text;
// .... do something with value here
}
I suggest you go through some tutorials though to get the hang of how to use GridView.
http://www.asp.net/web-forms/videos/building-20-applications/lesson-8-working-with-the-gridview-and-formview
http://www.aspsnippets.com/Articles/How-to-get-Selected-Row-cell-value-from-GridView-in-ASPNet.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview%28v=vs.110%29.aspx
You have to first read data from cells and then insert them into database using SqlCommand.
Assuming that you have M_ID and M_NAME columns in your Machining_Master table you can insert values to database as below:
//Assuming that your id column is first column and name is second column
//get value of id and name
int mId = Convert.ToInt32(GridView2.SelectedRow.Cells[0].Text);
string mName = GridView2.SelectedRow.Cells[1].Text;
string connectionStrng = "your connection string";
string insertSql = "INSERT INTO Machining_Master (M_ID, M_NAME) VALUES (#mId, #mName)";
using (SqlConnection conn = new SqlConnection(connectionStrng))
{
using (SqlCommand cmd = new SqlCommand(insertSql, conn))
{
try
{
cmd.Parameters.Add(new SqlParameter("mId", mId));
cmd.Parameters.Add(new SqlParameter("mName", mName));
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
//Close connection
conn.Close();
}
}
}
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 ...