PDS8475
Active member
- Joined
- Jun 25, 2019
- Messages
- 41
- Programming Experience
- Beginner
I have a database table called Sites with to columns SiteName and SiteAddress
The following code is on a button click event.
I have removed the database password from the connection string intentionally before posting.
When I step through the code, I can see the connection state opens.
However the reader remains null and when the ExecuteReader is called it jumps to the catch block.
the catch block gives the exception message "Failed due to No value given for one or more required parameters"
It appears to me that the query is wrong and therefore the reader can't read, But I can not see how it is wrong. I tried copying and pasting the names of the Table and columns into the query to make sure that I have them exactly right, I've also checked with other update queries online and it seems to be right.
Could anybody please tell me what is going wrong as I have spent two days on this, trying different things and I am getting no where.
The following code is on a button click event.
I have removed the database password from the connection string intentionally before posting.
When I step through the code, I can see the connection state opens.
However the reader remains null and when the ExecuteReader is called it jumps to the catch block.
the catch block gives the exception message "Failed due to No value given for one or more required parameters"
It appears to me that the query is wrong and therefore the reader can't read, But I can not see how it is wrong. I tried copying and pasting the names of the Table and columns into the query to make sure that I have them exactly right, I've also checked with other update queries online and it seems to be right.
Could anybody please tell me what is going wrong as I have spent two days on this, trying different things and I am getting no where.
C#:
string sn = SiteName_textBox.Text;
string sa = SiteAddress_textBox.Text;
try
{
OleDbDataReader read;
OleDbConnection connection = new OleDbConnection(@"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = |DataDirectory|\Admins.mdb; Jet OLEDB:Database Password = ");
string my_query = "UPDATE Sites SET SiteAddress = @sa WHERE SiteName = @sn";
OleDbCommand comm = new OleDbCommand(my_query, connection);
connection.Open();
read = comm.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
comm.Parameters.AddWithValue("@sn", SiteName_textBox.Text);
comm.ExecuteNonQuery();
}
}
read.Close();
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Failed due to " + ex.Message);
}
Last edited: