Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

I need help with a SQL Insert staement with paramaters.

I need help with a SQL Insert staement with paramaters. The insert is called from a button on an ASP.NET page.

This is what I have:
Protected Sub btnNewQuoteHist_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles btnNewQuoteHist.Click
         Dim conStockSelector As SqlConnection

        Dim strConnection As String = "Data Source=BURCEL05;Initial Catalog=StockSelectSQL;" _
        & "Integrated Security=SSPI"
        lblFileDate.Text = Me.calQuoteDate.SelectedDate
        conStockSelector = New SqlConnection(strConnection)
        conStockSelector.Open()
        Dim strInsert As String
        strInsert = "INSERT INTO DownLoadHistory " _
        & " VALUES (@DL_FileName,@DL_Date,MarketID,@DL_Appended);"
        Dim cmdSQL As SqlCommand = New SqlCommand(strInsert, conStockSelector)
        cmdSQL.CommandText = strInsert

        cmdSQL.Parameters.Add("@DL_FileName", Data.SqlDbType.NVarChar).Value = "ABC.txt"
        cmdSQL.Parameters.Add("@DL_Date", Data.SqlDbType.DateTime).Value = calQuoteDate.SelectedDate
        cmdSQL.Parameters.Add("@MarketID", Data.SqlDbType.SmallInt).Value = 2
        cmdSQL.Parameters.Add("@DL_Appended", Data.SqlDbType.Bit).Value = 0
        cmdSQL.ExecuteNonQuery()
        'Error The name "MarketID" is not permitted in this context
        'Column names are not permitted
        conStockSelector.Close()

I just need a hint on one of the Parameters.Add variables.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChillAmmar
ChillAmmar

Try this .. this should work...

replaces the "@" with ":"

strInsert = "INSERT INTO DownLoadHistory " _ & "VALUES :DL_FileName, :DL_Date, :MarketID, :DL_Appended)"

cmdSQL.Parameters.Add("DL_FileName", Data.SqlDbType.NVarChar).Value = "ABC.txt"
cmdSQL.Parameters.Add("DL_Date", Data.SqlDbType.DateTime).Value = calQuoteDate.SelectedDate
cmdSQL.Parameters.Add("MarketID", Data.SqlDbType.SmallInt).Value = 2
cmdSQL.Parameters.Add("DL_Appended", Data.SqlDbType.Bit).Value = 0
Avatar of Dovberman

ASKER

TimCottee
How simple. I wondered why only MarketID was the error source. I guess I should not have gotten up at 2 AM to work on this.

Thanks,
Always a bad idea to work at 2am. There are far better things to be doing at that time of day, not least sleeping!