Link to home
Create AccountLog in
Avatar of jjrr007
jjrr007

asked on

If - Else Statement in C#

I am working on a if/else statement that is generating erors

I have listed the code that I'm using in the code snippet when the form loads.  I'm not sure what I can do to remove the syntax errors and have it do the following on load:

1. Fill the form's data (THIS PART IS WORKING)
2. If the value of the list box called ReqID is Null (empty), end the program.  
2. If the value of the list Box called ReqID is Not Null then click a button called btnClick
3. Execute a query
4. Re-execute step 1

I'm missing the steps to click on the button- I don't know how to do this.  The error messages that I'm receiving is, "Cannot implicitly convert type 'string' to 'bool'". This occured on the line with, if (RecordedID.Text= "")

I would really appreciate if someone could assist me with this.  Thanks!

private void MainForm_Load(object sender, EventArgs e)
{
// This line of code loads data into the  table. 
 this.recordsToBeConvertedTableAdapter.Fill(this.sysDataDataSet1.RecordsToBeConverted);
if (RecordedID.Text= "")
{
this.Dispose();  
}
else
 {
SqlConnection connection = new SqlConnection(@"provider=Microsoft.Jet.OLEDB.4.0;data source = C:\path to access database\SysData.mdb");
connection.Open();
// Create a SqlCommand to retrieve Suppliers data.
SqlCommand command = new SqlCommand("Update Outbound SET RecordedID = '123' Where ReqID = @Col", connection);
command.CommandType = CommandType.Text;
 
SqlParameter param = new SqlParameter();
param.ParameterName = "@Col";
param.Value = RecordedID.Text; // RecordID is the name of the text box
 
command.Parameters.Add(param);
command.ExecuteNonQuery();
                this.recordsToBeConvertedTableAdapter.Fill(this.sysDataDataSet1.RecordsToBeConverted);
}
}

Open in new window

SOLUTION
Avatar of Limbeck
Limbeck

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of jjrr007
jjrr007

ASKER

Thanks.  When I changed the code to if (RecordedID.Text== ""), I received the following error message when I debugged the application:

Argument Exception was unhandled. Keyword not supported 'provider'
This occured on the line:
SqlConnection connection = new SqlConnection(@"provider=Microsoft.Jet.OLEDB.4.0;data source = C:\path to access database\SysData.mdb");

What do you suggest I change to have this work?  Also, I don't know how I can add the button click to this.  Thanks again!
If you want to connect to an access database, you can't use the SqlConnection object in that way. I believe you want to use the OleDbConnection class:

http://msdn2.microsoft.com/en-us/library/5ybdbtte(VS.71).aspx
ZachSmith is correct. Use this code for the update of a MS Access database.
OleDbConnection connection = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;data source = C:\path to access database\SysData.mdb");
			connection.Open();
			
			OleDbCommand command = new OleDbCommand("Update Outbound SET RecordedID = '123' Where ReqID = @Col", connection);
			command.CommandType = CommandType.Text;
			 
			OleDbParameter param = new OleDbParameter();
			param.ParameterName = "@Col";
			param.Value = RecordedID.Text; // RecordID is the name of the text box
			 
			command.Parameters.Add(param);
			command.ExecuteNonQuery();

Open in new window

Avatar of jjrr007

ASKER

jandromeda,

When I used the code you have.  It didn't give me any errors, however the data did not update (query didn't run).  When I put in the actual value of the parameter in the query, the query ran successfully. For example, the following ran successfully,

OleDbCommand command = new OleDbCommand("Update Outbound SET RecordedID = '123' Where ReqID = 1745", connection);

Using the parameter @Col in the above line of code (as written in your snippet), the query did not run.  So the paameter is not sending the value to the query.  BTW, the RecordedID value is a number.What do you suggest?  

Thanks again!
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of jjrr007

ASKER

Thanks a lot for your time.
Try adding the Type Propety for the param object:

                        OleDbParameter param = new OleDbParameter();
                        param.ParameterName = "@Col";
                        param.dbType = dbType.Integer;
                        param.Value = Integer.Parse(RecordedID.Text); // RecordID is the name of the text box - convert the string text to a Double type value

AW