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
<%@ 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>
</td>
</tr>
<tr>
<td align="right" class="style2">
name</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td align="right" class="style2">
city</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td align="right" class="style2">
age</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td align="right" class="style2">
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="insert" />
</td>
<td>
</td>
</tr>
<tr>
<td align="right" class="style2">
</td>
<td>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
my web config file
<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>