Incorrect syntax near the keyword 'FROM'. Incorrect syntax near ')'.]

Posted by Suniluc under ASP.NET on 2/5/2013 | Points: 10 | Views : 3494 | Status : [Member] | Replies : 15
protected void drpVno_SelectedIndexChanged(object sender, EventArgs e)
{
bool temp = false;
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\vip\\BVIS\\BVIS\\App_Data\\VehicleDb.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from tblalloc where VNo='" + drpVno.Text.Trim() + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
drpVno.Text = dr.GetValue(0).ToString();
drpEmpCode.Text = dr.GetString(2).ToString();
drpDrCode.Text = dr.GetString(1).ToString();
drpBrCode.Text = dr.GetString(3).ToString();
drpPrps.Text = dr.GetString(4).ToString();

temp = true;
}
if (temp == false)
MessageBox.Show("not found");
con.Close();
}




Responses

Posted by: Vuyiswamb on: 2/5/2013 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
What is the value of "drpVno.Text.Trim()" ?



Thank you for posting at Dotnetfunda
[Administrator]

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 2/5/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Please post the entire code with database

If this post helps you mark it as answer
Thanks

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
i want to retreive values from vehicles allocation based on selected vehicle no. fro from dropdown list

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
namespace BVIS
{
public partial class Allocation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}


protected void btnCncl_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}

protected void btnSave_Click(object sender, EventArgs e)
{
String connectionString = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlConnection con = new SqlConnection(connectionString);
string sql = "INSERT INTO [tblalloc] (VNo,EmpCode,Drcode,BrCode,Purpose) VALUES (@Val1,@Val2,@Val3,@Val4,@Val5)";


try
{
con.Open();


SqlCommand cmd = new SqlCommand(sql, con);

cmd.Parameters.AddWithValue("@Val1", drpVno.Text);

string ctgry = RdCtgry.SelectedItem.Value.ToString();
if (ctgry == "Commercial")
{
cmd.Parameters.AddWithValue("@Val2", "");

}
else
{

cmd.Parameters.AddWithValue("@Val2", drpEmpCode.Text);
}


cmd.Parameters.AddWithValue("@Val3", drpDrCode.Text);
cmd.Parameters.AddWithValue("@Val4", drpBrCode.Text);
cmd.Parameters.AddWithValue("@Val5", drpPrps.Text);
cmd.ExecuteNonQuery();

lblMsg.Text = "Insertion Successful";
drpVno.Focus();



}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}

protected void btnNew_Click(object sender, EventArgs e)
{
lblNew.Visible = true;
txtNew.Visible = true;
btnOk.Visible = true;

}

protected void btnOk_Click(object sender, EventArgs e)
{
drpPrps.Items.Add("txtNew.Text");
lblNew.Visible = false;
txtNew.Visible = false;
btnOk.Visible = false;

}

protected void RdCtgry_SelectedIndexChanged(object sender, EventArgs e)
{
string ctgry = RdCtgry.SelectedItem.Value.ToString();
if (ctgry == "Commercial")
{
drpEmpCode.Enabled = false;
}
else
{
drpEmpCode.Enabled = true;

}
}

protected void btnUpdt_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\vip\\BVIS\\BVIS\\App_Data\\VehicleDb.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "UPDATE [tblalloc] SET VNo = @vn, Drcode = @dr, EmpCode= @emp, BrCode=@br, Purpose=@pr WHERE VNo = @vn";

cmd.Parameters.AddWithValue("@vn", drpVno.Text);
cmd.Parameters.AddWithValue("@dr", drpDrCode.Text);
cmd.Parameters.AddWithValue("@emp", drpEmpCode.Text);
cmd.Parameters.AddWithValue("@br", drpBrCode.Text);
cmd.Parameters.AddWithValue("@pr", drpPrps.Text);

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

protected void drpVno_SelectedIndexChanged(object sender, EventArgs e)
{
bool temp = false;
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\vip\\BVIS\\BVIS\\App_Data\\VehicleDb.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from [tblalloc] where VNo='" + drpVno.Text.Trim() + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
drpVno.Text = dr.GetValue(0).ToString();
drpEmpCode.Text = dr.GetString(2).ToString();
drpDrCode.Text = dr.GetString(1).ToString();
drpBrCode.Text = dr.GetString(3).ToString();
drpPrps.Text = dr.GetString(4).ToString();

temp = true;
}
if (temp == false)
MessageBox.Show("Not found");
con.Close();
}


}
}


Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
plz reply....

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pavanandey on: 2/5/2013 [Member] Bronze | Points: 25

Up
0
Down
Check what value are you passing to VNo='" + drpVno.Text.Trim() + "'", looks like vno takes int and you are passing text

Thanks
Pavan Kumar
Mark Answer if this fits the need

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
data type of vno is varchar2

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 2/5/2013 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
please don't post things that are unnecessary , post the line of code where the problem happens and i asked you what is the value of your variable and you did not reply and you expect us to give you an answer to your problem.


Thank you for posting at Dotnetfunda
[Administrator]

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
VNo-KL-07-BU-4952
DriverCode-DR1
EmpCode-1E01022009
BranchCode-EKM 3
Purpose-Personal
The value passed is given in bold...vehicle no

i appreciate your quick replies...thank u



Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
i have the value passed, then what happened?

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 2/5/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication13._Default" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:DropDownList ID="drpVno" runat="server"
onselectedindexchanged="drpVno_SelectedIndexChanged"></asp:DropDownList><br />
<asp:DropDownList ID="drpEmpCode" runat="server"></asp:DropDownList><br />
<asp:DropDownList ID="drpDrCode" runat="server"></asp:DropDownList><br />
<asp:DropDownList ID="drpBrCode" runat="server"></asp:DropDownList><br />
<asp:DropDownList ID="drpPrps" runat="server"></asp:DropDownList>

</div>


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




using System;
using System.Collections;
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.SqlClient;
namespace WebApplication13
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;

protected void Page_Load(object sender, EventArgs e)
{
drpVno.AutoPostBack = true;
SqlConnection con = new SqlConnection(strConnString);

if (!IsPostBack)
{
drpVno.Items.Add("Choose Vno");
con.Open();
str = "select * from tblalloc";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
drpVno.Items.Add(reader["Vno"].ToString());
}
reader.Close();
con.Close();
}
}




protected void drpVno_SelectedIndexChanged(object sender, EventArgs e)
{
clear();
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from tblalloc where Vno='" + drpVno.SelectedItem.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
drpEmpCode.Items.Add(reader["EmpCode"].ToString());
drpDrCode.Items.Add(reader["DrCode"].ToString());
drpBrCode.Items.Add(reader["BrCode"].ToString());
drpPrps.Items.Add(reader["Prps"].ToString());
}
reader.Close();
con.Close();
}
private void clear()
{
drpEmpCode.Items.Clear();
drpDrCode.Items.Clear();
drpBrCode.Items.Clear();
drpPrps.Items.Clear();
}
}
}


If this post helps you mark it as answer
Thanks

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks a lot Satyapriyanayak ,but when i debug,the same mistake is shown

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
Server Error in '/' Application.
--------------------------------------------------------------------------------

Incorrect syntax near the keyword 'FROM'.
Incorrect syntax near ')'.
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 the keyword 'FROM'.
Incorrect syntax near ')'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SqlException (0x80131904): Incorrect syntax near the keyword 'FROM'.
Incorrect syntax near ')'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2030802
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5009584
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33
System.Data.SqlClient.SqlDataReader.get_MetaData() +86
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +144
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +319
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) +92
System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1618
System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +97
System.Web.UI.WebControls.ListControl.PerformSelect() +34
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +29
System.Web.UI.Control.PreRenderRecursiveInternal() +103
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Control.PreRenderRecursiveInternal() +175
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496




Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
why this error is shown? plz help me...

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suniluc on: 2/5/2013 [Member] Starter | Points: 25

Up
0
Down
i thank you all for trying to help me...i corrected the error...error was due to conection failure in datasource configuration....thank u all once again

Suniluc, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response