Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
System.Data.SqlClient.SqlException was unhandled by user code
  Message="Incorrect syntax near ')'."
  Source=".Net SqlClient Data Provider"
  ErrorCode=-2146232060
  Class=15
  LineNumber=1
  Number=102
  Procedure=""
  Server="\\\\.\\pipe\\3A2A659F-F9B2-41\\tsql\\query"
  State=1
  StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at _Default.Button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\kachu\My Documents\Visual Studio 2008\WebSites\newimginsert\Default.aspx.cs:line 44
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

this is my code
C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
   //   SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFileName=|DataDirectory|\\ImageDB.mdf; integrated security=true;user instance=true;");
    SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDBFileName=|DataDirectory|\\ImageDB.mdf; integrated security=true;user instance=true;");

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            //byte[] img = new byte[FileUpload1, postedfile.contentLength];
            byte[] img = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile myimg = FileUpload1.PostedFile;
            myimg.InputStream.Read(img, 0, FileUpload1.PostedFile.ContentLength);
            SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,)", con);
            
            SqlParameter imgname = new SqlParameter("@imagename", SqlDbType.VarChar,25);
            imgname.Value = TextBox1.Text;
            cmd.Parameters.Add(imgname);
            
            SqlParameter uploading = new SqlParameter("@image", SqlDbType.Image);
            uploading.Value = img;
            cmd.Parameters.Add(uploading);
            
            con.Open();
            
            cmd.ExecuteNonQuery();
            
            con.Close();
            
            Label1.Text = "image uploaded........";


        }
        else
            Label1.Text = "can't upload file.........";
    }
}

n i am following video
http://www.youtube.com/watch?v=HYdsHttQzDw


plz help me thanks in advance and my prob is i want to insert and retrieve image from sql and to retrieve image to gridview i am not getting correct code so working on it from past 4 days but still not able to do..:-(


[edit]Code blocks added - OriginalGriff[/edit]
Posted
Updated 3-Sep-18 8:44am
v2
Comments
Member 13047328 16-Apr-17 2:15am    
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Web.Configuration

Partial Class AdminProd
Inherits System.Web.UI.Page
Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=E:\988\stationary_project\App_Data\dbstet.mdf;Integrated Security=True;User Instance=True")
Dim da As New SqlDataAdapter("select * from Product order by Item_Id", conn)
Dim ds As New DataSet
Dim dt As New DataTable
Dim m As Integer
Dim img1, usr As String
Protected Sub page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim da2 As New SqlDataAdapter("select Cat_name from Category", conn)
Dim ds2 As New DataSet
ds2.Clear()
da2.Fill(ds2, "Category")
ddlcode.Items.Clear()
ddlcode.Items.Add("Select")
For j = 0 To ds2.Tables("Category").Rows.Count - 1
ddlcode.Items.Add(ds2.Tables("Category").Rows(j).Item(0))
Next
End Sub




Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Session.Count > 0 Then
usr = Session("username").ToString
Else
Response.Redirect("login.aspx")

End If
conn.Open()
ds.Clear()
da.Fill(ds, "Product")
GridView1.DataSource = ds
GridView1.DataBind()
GridView1.AllowPaging = True
GridView1.PageSize = 5
End Sub



Protected Sub ddlcode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlcode.TextChanged
Dim da22 As New SqlDataAdapter("select Cat_code from Category where Cat_name='" & ddlcode.SelectedItem.ToString & "'", conn)
Dim ds22 As New DataSet
ds22.Clear()
da22.Fill(ds22, "Category")
If (dt.Rows.Count > 0) Then
lblccode.Text = ds22.Tables("Category").Rows(0).Item(0)

End If



End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
GridView1.DataSource = ds
GridView1.DataBind()
End Sub




Protected Sub btnadd_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnadd.Click
lblid.Text = ""
lblccode.Text = ""

txtnm.Text = ""
txtprice.Text = ""


Image2.ImageUrl = ""

dt = New DataTable
dt.Clear()
da.Fill(dt)
Dim n As Integer = dt.Rows.Count - 1

If dt.Rows.Count = 0 Then
m = 1
Else
m = Val(dt.Rows(n).Item(0).ToString) + 1
End If

lblid.Text = m
ddlcode.ClearSelection()
btnadd.Enabled = False
btnsave.Enabled = True
btnup.Enabled = False
btndel.Enabled = False


End Sub

Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsave.Click
' If ddlcode.SelectedItem.Text = "Select" Then
'MsgBox("Please select category")
'ElseIf lblccode.Text = "" Then
'MsgBox("Please select category again")
'ElseIf txtnm.Text = "" Then
'MsgBox("Please enter product name")
'ElseIf txtprice.Text = "" Then
'MsgBox("Please enter product price")
'ElseIf FileUpload2.FileName = "" Then
'MsgBox("Please upload product image")
'Else


Dim img1 As String = Path.GetFileName(FileUpload2.PostedFile.FileName)
FileUpload2.SaveAs(Server.MapPath("~/image/" & img1))
img1 = "image/" & img1
Dim S As String
S = "insert into Product values(" & lblid.Text & ",'" & txtnm.Text & "'," & lblccode.Text & "," & txtprice.Text & ",'" & img1 & "')"

Dim CMD As New SqlCommand(S, conn)
CMD.ExecuteNonQuery()
ds.Clear()
da.Fill(ds, "Product")

GridView1.DataSource = ds
GridV
Member 13047328 16-Apr-17 2:17am    
i have above code btnsave but it don't parse if condition to else part plz help

Your query is incorrect. Try to copy the query from here and run it in SQL Server Management Studio. It will give you an idea on what syntax is incorrect.
 
Share this answer
 
Comments
Monjurul Habib 6-Apr-12 5:13am    
5!
Abhinav S 6-Apr-12 6:29am    
Thank you Monjurul.
Take out the comma.
C#
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,)", con);
Becomes
C#
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename)", con);



[edit]
You might actually want to add the other parameter instead:
C#
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,@image)", con);

[/edit]
 
Share this answer
 
v2
Comments
karthikh87 6-Apr-12 3:31am    
i had forgot to write "@image" after comma sir.. and thanks for the great support
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,@image)", con);

and i wish and request for the same support al the time and its great to be in this forum
thanks once again and if possible can u please give the code to display image in grid view as i am not getting from many days m sorry if i am expecting more m very new to this development sir..
OriginalGriff 6-Apr-12 3:35am    
You're welcome!
Have a look at this article - it does just that.
http://www.codeproject.com/Articles/268123/Display-Store-and-Retrieve-Image-Data-from-Databas
Monjurul Habib 6-Apr-12 5:13am    
5!
Dear friend,

Please check this line
C#
SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,)", con);


Please remove bold semicolon

may be your problem will solve.


best regards,
Anil Avhad
 
Share this answer
 
v2
Incorrect query.
check the syntax and run it in SQL..

you are just passing only one value ..plz check it

SqlCommand cmd = new SqlCommand("insert into myimage(ImageName,Image) values(@imagename,)", con);
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900