This question already has answers here:
Read data from SqlDataReader
(13 answers)
Closed 2 years ago.
I'm trying to read data from the data source and I got this error in the ExecuteReader method,
When I try to show the result in some label.
I'm getting the error here idlabel.Text = myraeder("id"); under myreader varibale
private void Searchbtn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(dataConnectin);
con.Open();
string searchsql = "SELECT * from Table WHERE Name=" + searchtxt.Text + "";
SqlCommand cmd = new SqlCommand(searchsql, con);
SqlDataReader myraeder;
myraeder = cmd.ExecuteReader();
myraeder.Read();
idlabel.Text = myraeder("id");
}
Use square brackets instead of round brackets.
Convert the field to string.
idlabel.Text = myraeder["id"].ToString();
Read the data in this way:
idlabel.Text = myraeder["id"].ToString();
con.Close();
And do not forget to close the connection after getting required data.
There are so many errors that are obvious. Let me show you a better way to write code. Follow me!
Give the variables proper names and casing.
Use asynchrony.
Use parameters in sql queries.
Release resources by wrapping them in using.
Enclose names in brackets in sql.
private async void SearchButton_Click(object sender, EventArgs e)
{
using (var connection = new SqlConnection(connectionString))
{
await connection.OpenAsync();
string searchSql = "SELECT * from [Table] WHERE [Name]=#name";
using (var command = new SqlCommand(searchSql, connection))
{
command.Parameters.Add("name", SqlDbType.NVarChar).Value = searchTextBox.Text;
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
{
idLabel.Text = reader["id"].ToString();
}
}
}
}
}
Related
I have this code:
private void dataGridView2_CellClick(object sender,DataGridViewCellEventArgs e)
{
cmd.Connection = cn;
cmd.CommandText = "select * from subdiseases where id_subdiss=" + dataGridView2.SelectedRows[0].Cells[0].Value;
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
cn.Open();
SqlDataReader dr4 = null;
dr4 = cmd.ExecuteReader() ;
while (dr4.Read())
{
a = dr4["id_diss"].ToString();
comboBox1.Text = dr4["name_diss"].ToString();
namesub.Text = dr4["name_subdiss"].ToString();
namedet.Text = dr4["details"].ToString();
}
dr4.Close();
}
I get this error:
Invalid attempt to call Read when reader is closed.
As someone mentioned in a comment below your post, make sure that you're getting this error on the very method that you posted, as it looks fine to me. Normally you would get this error if you closed the reader inside your loop or if you tried reading it on a closed db connection. You might want to try to just open the connection once at the beginning and closing it once at the end, removing that open close validation you have, although that shouldn't be the issue.
This question already has an answer here:
.NET OleDb parameterized query not working as expected
(1 answer)
Closed 6 years ago.
I'm trying to query a table, but I get an error when I execute it, it only happens when I add "Where Sdate=#date and Staffid=#mobile" if I don't give a condition it works fine, here is the code
private void button1_Click(object sender, EventArgs e)
{
getsum();
}
public void getsum()
{
string query = #"select SUM(SQuantity) AS Total FROM Sales where Sdate=#date AND Staffid=#mobile";
using (var conn = new System.Data.OleDb.OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
using (var cmd = new System.Data.OleDb.OleDbCommand(query, conn))
{
cmd.Parameters.Add("#mobile", System.Data.OleDb.OleDbType.VarChar).Value = '1';
cmd.Parameters.Add("#date", System.Data.OleDb.OleDbType.VarChar).Value = dateTimePicker1.Value.Date;
conn.Open();
using (var rdr = cmd.ExecuteReader())
{
if (rdr.Read())
{
textBox1.Text = rdr["Total"].ToString();
}
rdr.Close();
}
}
}
Hope anyone can help me fix this
Try with other data types:
cmd.Parameters.Add("#mobile", System.Data.OleDb.OleDbType.Integer).Value = 1;
cmd.Parameters.Add("#date", System.Data.OleDb.OleDbType.DBDate).Value = dateTimePicker1.Value.Date;
I am attempting to change my code. I am in the process of learning while I code and many parts of my C#/ASP.Net application use LINQ to SQL and many parts use commands to directly access the sql database. I felt like I should standardize these, so I am changing over the LINQ to SQL code. I have a portion of the code that takes the finds one record based on a session variable and then populates the labels in my header with the sql query results. here is the code:
protected void Page_Load(object sender, EventArgs e)
{
var pinNum = MySession.Current.focusParcel;
if (pinNum != 0)
{
String sql = ConfigurationManager.ConnectionStrings["Recorder"].ConnectionString;
SqlConnection connection = new SqlConnection(sql);
connection.Open();
SqlCommand command = new SqlCommand("Select PARCEL, PIN_TXT, Owner, Address1, Address2, CSZ, ACRES, LEGAL, Active FROM[ParcelView] WHERE PARCEL = " + pinNum, connection);
SqlDataReader selectedParcel = command.ExecuteReader();
if (selectedParcel != null)
{
lblPIN_Num.Text = selectedParcel["PARCEL"].ToString();
lblPIN_TXT.Text = selectedParcel["PIN_TXT"].ToString();
lblDateTime.Text = DateTime.Now.ToString("MMMM dd, yyyy");
lblOwner.Text = selectedParcel["Owner"].ToString();
lblAddress1.Text = selectedParcel["Address1"].ToString();
lblAddress2.Text = selectedParcel["Address2"].ToString();
lblCSZ.Text = selectedParcel["CSZ"].ToString();
lblAcres.Text = string.Format("{0} Acres", selectedParcel["ACRES"]);
lblLegal.Text = selectedParcel["LEGAL"].ToString();
if (selectedParcel["Active"].ToString() == "A")
{
lblInactive.Text = " (ACTIVE)";
}
else
{
lblInactive.Text = " (INACTIVE)";
lnkAddDocument.Visible = false;
}
}
lblCurrentUser.Text = Page.User.Identity.Name;
connection.Close();
}
else
Response.Redirect("./ParcelSearch.aspx");
}
I am receiving the following error:
Invalid attempt to read when no data is present.
I know the SQL statement used will return a record when it is used directly into sql query of database.
Thanks in advance for any advice. Should I be concerned about the lack of consistency in accessing data across the application at all? If so, should I be converting everything to LINQ? Or converting everything away from LINQ? I klnow of the existence of MVC and know I should learn it and use it, but I am too far down the path to try to convert there right now.
Change
if (selectedParcel != null)
To
if (selectedParcel.Read())
You have to read a record in the data reader before you can start pulling data. Its a forward only cursor that starts off not pointing to a record. The Read() method advances the reader by 1 and returns true/false if it was successful.
Note that it is not necessary to call HasRows if use the code above. The if block will only be entered if there is a row, if there is not then Read returns false and the block is not entered.
Edit
There are many bad practices in your code.
Use parameterized sql!! This is the most important thing you can take away here!
Wrap your connections in using blocks to ensure the connection is ALWAYS closed.
Sql code changes
String sql = ConfigurationManager.ConnectionStrings["Recorder"].ConnectionString;
using(SqlConnection connection = new SqlConnection(sql))
using(SqlCommand command = new SqlCommand("Select PARCEL, PIN_TXT, Owner, Address1, Address2, CSZ, ACRES, LEGAL, Active FROM [ParcelView] WHERE PARCEL = #pinNum", connection))
{
command.Parameters.Add(new SqlParameter("#pinNum", SqlDbType.VarChar){Value = pinNum});
connection.Open();
using(SqlDataReader selectedParcel = command.ExecuteReader())
{
if (selectedParcel.Read())
{
/*code unchanged*/
}
}
/* 1 line code unchanged*/
}
Once you have executed the command you need to actually read the data :
SqlDataReader selectedParcel = command.ExecuteReader();
if (selectedParcel.HasRows)
{
selectedParcel.Read();
....
....
I recommend you use this format for your sqlDatareader instead, as it it will parse whether results have been retrieved or not, and will automatically dispose itself on exception:
using(SqlDataReader selectedParcel = command.ExecuteReader())
{
while (selectedParcel .Read())
{
//Assign label text to results
}
}
You should Read() the SqlDataReader.
protected void Page_Load(object sender, EventArgs e)
{
var pinNum = MySession.Current.focusParcel;
if (pinNum == 0)
Response.Redirect("./ParcelSearch.aspx");
String sql = ConfigurationManager.ConnectionStrings["Recorder"].ConnectionString;
using(SqlConnection connection = new SqlConnection(sql))
{
connection.Open();
SqlCommand command = new SqlCommand("Select PARCEL, PIN_TXT, Owner, Address1, Address2, CSZ, ACRES, LEGAL, Active FROM[ParcelView] WHERE PARCEL =#PinNum ", connection);
//protect from sql injection
command.Parameters.AddWithValue(#PinNum, pinNum);
using(SqlDataReader selectedParcel = command.ExecuteReader())
{
if(!selectedParcel.HasRows)
return;
SetLabels(selectedParcel);
}
}
}
private void SetLabels(SqlDataReader selectedParcel)
{
lblPIN_Num.Text = selectedParcel["PARCEL"].ToString();
lblPIN_TXT.Text = selectedParcel["PIN_TXT"].ToString();
lblDateTime.Text = DateTime.Now.ToString("MMMM dd, yyyy");
lblOwner.Text = selectedParcel["Owner"].ToString();
lblAddress1.Text = selectedParcel["Address1"].ToString();
lblAddress2.Text = selectedParcel["Address2"].ToString();
lblCSZ.Text = selectedParcel["CSZ"].ToString();
lblAcres.Text = string.Format("{0} Acres", selectedParcel["ACRES"]);
lblLegal.Text = selectedParcel["LEGAL"].ToString();
if (selectedParcel["Active"].ToString() == "A")
{
lblInactive.Text = " (ACTIVE)";
}
else
{
lblInactive.Text = " (INACTIVE)";
lnkAddDocument.Visible = false;
}
lblCurrentUser.Text = Page.User.Identity.Name;
}
I refactor your code to look a little bit. You had different problems as not closing your Reader. You was open to sql injection, also you should transfer the connection to Db in separate class.
I am completely blind with this Error. I'm more of a Java man than an ASP one. So here is my code and my question :
Here is my code that produce the error :
protected void ButtonOk_Click(object sender, EventArgs e)
{
// OracleConnection connect = new OracleConnection();
// connect.ConnectionString = ConfigurationManager.ConnectionStrings["Absensi2.Properties.Settings.Setting"].ConnectionString;
//// SqlConnection connect = new SqlConnection(getConnection());
var sql = "insert into master_dosen('NIP','NAMA_DOSEN','KETERANGAN') values (:NIP, :NAMA_DOSEN, :KETERANGAN)";
using (OracleConnection c = new OracleConnection(ConfigurationManager.ConnectionStrings["Absensi2.Properties.Settings.Setting"].ConnectionString))
{
c.Open();
using (OracleCommand cmd = new OracleCommand(sql, c))
{
cmd.Parameters.Add(":NIP", TextBoxNIP.Text);
cmd.Parameters.Add(":NAMA_DOSEN", TextBoxNamaDosen.Text);
cmd.Parameters.Add(":KETERANGAN", TextBoxKeterangan.Text);
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
c.Close();
}
The error is :
ORA-00928: missing SELECT keyword
in line cmd.ExecuteNonQuery();
I already search and it says deprecated. is it true?
NB : I am using ODP.NET, and I am using visual studio 2010.
The problem with your query is that you are wrapping column names with single quotes which makes them string literal.
To fix the problem, just remove the single quotes around the column names:
var sql = "insert into master_dosen(NIP,NAMA_DOSEN,KETERANGAN) ...";
protected void ButtonOk_Click(object sender, EventArgs e)
{
// OracleConnection connect = new OracleConnection();
// connect.ConnectionString = ConfigurationManager.ConnectionStrings["Absensi2.Properties.Settings.Setting"].ConnectionString;
//// SqlConnection connect = new SqlConnection(getConnection());
var sql = "insert into master_dosen(NIP,NAMA_DOSEN,KETERANGAN) values (:NIP, :NAMA_DOSEN, :KETERANGAN)";
using (OracleConnection c = new OracleConnection(ConfigurationManager.ConnectionStrings["Absensi2.Properties.Settings.Setting"].ConnectionString))
{
c.Open();
using (OracleCommand cmd = new OracleCommand(sql, c))
{
cmd.Parameters.Add(":NIP", TextBoxNIP.Text);
cmd.Parameters.Add(":NAMA_DOSEN", TextBoxNamaDosen.Text);
cmd.Parameters.Add(":KETERANGAN", TextBoxKeterangan.Text);
cmd.ExecuteNonQuery();
GridView1.DataBind();
}
c.Close();
}
Try this it will work ,I think you miss the sql insert syntax
Below is code in which I have written only save first row value in database but when I try to save multiple row value it gives an error. I don't know how to use loop in this code and where to use loop to insert multiple rows in database at once.
How do I save DataGridView values into a SQL Server database?
private void button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("INSERT INTO datagrid (sr, name, email, tel_no) VALUES(#c1,#c2,#c3,#c4)", cs);
{
cmd.Parameters.Add(new SqlParameter("#C1", SqlDbType.VarChar));
cmd.Parameters.Add(new SqlParameter("#C2", SqlDbType.VarChar));
cmd.Parameters.Add(new SqlParameter("#C3", SqlDbType.VarChar));
cmd.Parameters.Add(new SqlParameter("#C4", SqlDbType.VarChar));
cs.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
{
if (!row.IsNewRow)
{
cmd.Parameters["#C1"].Value = row.Cells[0].Value;
cmd.Parameters["#C2"].Value = row.Cells[1].Value;
cmd.Parameters["#C3"].Value = row.Cells[2].Value;
cmd.Parameters["#C4"].Value = row.Cells[3].Value;
cmd.ExecuteNonQuery();
}
cs.Close();
}
}
}
}
There's some information that would be nice to have that you didn't put in your original question:
cs is most likely a SqlConnection, and since you say it works inserting one row I'm guessing it's a global (class level) variable and is created somewhere else. I would do a little bit differently (see my code example for the reason why).
What error are you getting? Based on the problem description, I'll bet it is a closed connection (because you're closing it at the end of your foreach code block, when you should be closing it outside the foreach code block).
So, with that said, here's something that might do the trick for you:
private void button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
cs.Open();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (!row.IsNewRow)
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO datagrid (sr, name, email, tel_no) VALUES(#c1,#c2,#c3,#c4)", conn))
{
cmd.Parameters.AddWithValue("#C1", row.Cells[0].Value);
cmd.Parameters.AddWithValue("#C2", row.Cells[1].Value);
cmd.Parameters.AddWithValue("#C3", row.Cells[2].Value);
cmd.Parameters.AddWithValue("#C4", row.Cells[3].Value);
cmd.ExecuteNonQuery();
}
}
}
}
}
In the example above, I use using blocks for the SqlConnection and SqlCommand objects. The using block ensures that the resources are properly cleaned up when the using block is exited - this way you don't have to worry about things like when to call conn.Close(), as the using block will take care of that for you.
Also, I create the SqlConnection in the method. The connectionString in this case would be a string variable (at the class level, so all methods that need it can access it) holding the connection string.
Finally, I create the SqlCommand in the if block, which will only be entered if the !row.IsNewRow expression evaluates to true. I'm not 100% sure you can reassign values to a an existing command's parameters (most likely you can, but...)
I would also suggest including some error handling in case something goes wrong with the insert.
Hopefully this addresses the questions/issues you have. If not, please provide more detail so we can better assist you. Happy coding!
The problem is that cs is closed more times than it is opened.
You open the connection, insert, but then try to close for each iteration.
Either you should open a connection for each iteration, or you should see how to do everything with only only connection.
int ab;
int PIrowss = dataGridView2.Rows.Count;
for (ab = 0; ab < PIrowss; ab++)
{
PiCGetAutID();
purchaeOrder.pcautoid = Catget;
purchaeOrder.ponum = label119.Text;
purchaeOrder.col1 = dataGridView2.Rows[ab].Cells[0].Value.ToString();
purchaeOrder.col2 = dataGridView2.Rows[ab].Cells[1].Value.ToString();
purchaeOrder.col3 = dataGridView2.Rows[ab].Cells[2].Value.ToString();
purchaeOrder.col4 = dataGridView2.Rows[ab].Cells[3].Value.ToString();
purchaeOrder.col5 = dataGridView2.Rows[ab].Cells[4].Value.ToString();
purchaeOrder.POcSave();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
string sr = null;
string name = null;
string email = null;
string tel_no = null;
for (int i = 0; i <= this.DataGridView1.Rows.Count; i++) {
sr == this.DataGridView1.Rows(i).Cells(0).Value.ToString()
name == this.DataGridView1.Rows(i).Cells(1).Value.ToString()
email == this.DataGridView1.Rows(i).Cells(2).Value.ToString()
tel_no == this.DataGridView1.Rows(i).Cells(3).Value.ToString()
comm.CommandText = "insert into mytable(name,age) values('" & name & "','" & age& "')"
comm.ExecuteNonQuery()
conn.Close()
}
}
Try this and see. It should work