tags:

views:

73

answers:

4
string updateIncomeData = @"INSERT INTO TEAM_FUNDS_DETAILS("
   + "COMPONENT_TYPE,COMPONENT_NAME,COMPONENT_AMOUNT, YEAR_FOR, MONTH_FOR)"
   + "VALUES(" + Convert.ToInt32(TeamFundDetailsEnumClass.ComponentType.Income) 
   + " , ?, ?," 
   + ddlYear.SelectedIndex + ", " + ddlMonth.SelectedIndex + ")"

This parametrized query gives me an exception that tells me that there is an error near "?". What is the error. Please correct it.

+1  A: 

I am purely guessing but should it be year.selecteditem? not selectedindex?

rlee923
can you post the table structure as well?
rlee923
selecteditem.text? maybe?
rlee923
A: 

I don't understand why you would want to mix up the parameter substitution.

Specify all five columns as parameters and set the values that way.

"INSERT INTO TEAM_FUNDS_DETAILS " +
"(COMPONENT_TYPE,COMPONENT_NAME,COMPONENT_AMOUNT, YEAR_FOR, MONTH_FOR) " +
"VALUES(? , ?, ?,?, ?)"
Ed Manet
@Ed Manet: still gives exception
Shalni
+2  A: 

You must set the parameterized values (the ones with the question mark). Here is a similar example in VB.NET:

' Make a Command for this connection
' and this transaction.
Dim cmd As New OleDb.OleDbCommand( _
    "SELECT * FROM People WHERE FirstName=? AND " & _
        "LastName=?", _
    connUsers)

' Create parameters for the query.
cmd.Parameters.Add(New _
    OleDb.OleDbParameter("FirstName", first_name))
cmd.Parameters.Add(New OleDb.OleDbParameter("LastName", _
    last_name))

If you don't want to use parameterized queries, just substitute the question mark with the default value, or the variable with the value:

string updateIncomeData = @"INSERT INTO TEAM_FUNDS_DETAILS("
   + "COMPONENT_TYPE,COMPONENT_NAME,COMPONENT_AMOUNT, YEAR_FOR, MONTH_FOR)"
   + "VALUES(" + Convert.ToInt32(TeamFundDetailsEnumClass.ComponentType.Income) 
   + " , '', 0," 
   + ddlYear.SelectedIndex + ", " + ddlMonth.SelectedIndex + ")"

or

string updateIncomeData = @"INSERT INTO TEAM_FUNDS_DETAILS("
   + "COMPONENT_TYPE,COMPONENT_NAME,COMPONENT_AMOUNT, YEAR_FOR, MONTH_FOR)"
   + "VALUES(" + Convert.ToInt32(TeamFundDetailsEnumClass.ComponentType.Income) 
   + " , '" + myComponentName + "', " + myComponentAmount," 
   + ddlYear.SelectedIndex + ", " + ddlMonth.SelectedIndex + ")"
greuze
A: 

ddlMonth.SelectedItem.Value

Popo