Click here to Skip to main content
15,561,948 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my code is showing error in
da.fill(dt);

please find the error and give me the solution


Incorrect syntax near '3'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '3'.

Source Error:


Line 29: dt = new DataTable();
Line 30: da = new SqlDataAdapter(cmd);
Line 31: da.Fill(dt);
Line 32: return dt;
Line 33:

Source File: g:\WebSite\WebSite1\App_Code\bal.cs Line: 31





my code is here



this is my bal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

///
/// Summary description for bal
///

public class bal
{
SqlCommand cmd;
SqlConnection con;
SqlDataAdapter da;
DataTable dt;
public bal()
{

con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());

}

public DataTable select()
{

cmd = new SqlCommand("select * from 3tier", con);
dt = new DataTable();
da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;

}
public void insert(dal dl)
{
cmd = new SqlCommand("insert into 3tier(name,city,age)values(@name,@city,@age)", con);

cmd.Parameters.AddWithValue("@name", dl.name);

cmd.Parameters.AddWithValue("@city", dl.city);

cmd.Parameters.AddWithValue("@age", dl.age);


con.Open();
cmd.ExecuteNonQuery();
con.Close();


}
}


my dal code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;

///
/// Summary description for dal
///

public class dal
{

public int Id { get; set; }
public string name { get; set; }
public string city { get; set; }
public int age { get; set; }




bal bl = new bal();
public DataTable select1()
{
return bl.select();


}
public void insert(dal dl)
{
bl.insert (dl);

}
}


my UI code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;

///
/// Summary description for dal
///

public class dal
{

public int Id { get; set; }
public string name { get; set; }
public string city { get; set; }
public int age { get; set; }




bal bl = new bal();
public DataTable select1()
{
return bl.select();


}
public void insert(dal dl)
{
bl.insert (dl);

}
}
my aspx page


XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="register.aspx.cs" Inherits="register" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 91px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
        <tr>
            <td align="right" class="style2">
                Id</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                name</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                city</td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                age</td>
            <td>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="insert" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                &nbsp;</td>
            <td>
                <asp:GridView ID="GridView1" runat="server">
                </asp:GridView>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>

    </div>
    </form>
</body>
</html>




my web config file

XML
<configuration>
    <connectionStrings>
        <add name="dbconnection" connectionString="Data Source=SUBHAN-PC;Integrated Security=true;Initial Catalog=subhan" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    </system.web>
</configuration>
Posted
Updated 1-Dec-22 0:33am
Comments
[no name] 11-Sep-14 7:21am    
Okay so fix your SQL query.

It's the table name:
C#
cmd = new SqlCommand("insert into 3tier(name,city,age)values(@name,@city,@age)", con);
SQL sees the '3' and assumes it's a number, so the rest of the tablename deosn;t fit any suitable syntax.
Try escaping the table name:
C#
cmd = new SqlCommand("insert into [3tier] (name,city,age)values(@name,@city,@age)", con);
 
Share this answer
 
correct your sqlqueries:

by writing table name like below

[tablename]


dont use numeric values at starting of any table name, or any other methods,procedures its a bad programming practice
 
Share this answer
 
Comments
/\jmot 11-Sep-14 15:00pm    
hmmm, but if sometimes need it then okk..
but don't forget to use [tabname].

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