2013-05-10 82 views
0
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="postreplyadmin.aspx.cs" Inherits="postreplay" MasterPageFile="~/AdminMaster.master" Title="Post-Reply Page"%> 
<%@ Import Namespace="System.Data" %> 
<%@ Import Namespace="System.Data.SqlClient" %> 

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
<% 
    String postid = Request.QueryString["id"]; 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["civilRegDB"].ConnectionString); 
    con.Open(); 
    String sql = "select * from Forumthread where PostID=" + postid; 
    SqlDataAdapter da=new SqlDataAdapter(sql,con);   
    DataSet ds=new DataSet(); 
    da.Fill(ds); 
    DataRow drow = ds.Tables[0].Rows[0]; 
    String name = drow["Name"].ToString(); 
    String desc = drow["Description"].ToString(); 
    DateTime dt = Convert.ToDateTime(drow["PostDate"].ToString()); 
    String postdate = dt.ToString("dd/MM/yyyy",System.Globalization.CultureInfo.InvariantCulture); 
    String mailid = drow["Email"].ToString(); 
    %> 
</asp:content> 

我收到一個sqlexception而我試圖回覆帖子。錯誤:'='附近語法不正確。我環顧四周尋找類似於我的其他問題,但我找不到任何值得我的東西。運行時異常語法錯誤'='

我在「da.fill(ds);」上收到錯誤消息。任何人都可以幫我這個... :(

回答

1

postid從查詢字符串可能不具有價值,其結果是TSQL語句無效。

添加的支票是否查詢字符串提供價值

也驗證它的值是你所期望的範圍,並考慮使用預處理語句/參數 - 。當前的方法是讓你打開到SQL注入

+0

有帖子ID值。但我仍然收到錯誤.. – user2369497 2013-05-10 10:11:12

+0

postid的值是多少? – Kami 2013-05-10 10:11:56

+0

其int數據類型 – user2369497 2013-05-10 10:19:12

2

postid是一個字符串,它應該是用單引號括起來'

String sql = "select * from Forumthread where PostID='" + postid +"'"; 

但是,如果您使用SqlParameter可以讓您免受SQL Injection的困擾,那就更好了。

像:

String postid = Request.QueryString["id"]; 
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["civilRegDB"].ConnectionString); 
con.Open(); 
String sql = "select * from Forumthread where [email protected]"; //parameter 
SqlCommand cmd = new SqlCommand(sql, con); 
cmd.Parameters.AddWithValue("postID", postid); 
SqlDataAdapter da = new SqlDataAdapter(cmd); // pass command to adapter 
DataSet ds = new DataSet(); 
da.Fill(ds); 
DataRow drow = ds.Tables[0].Rows[0]; 
String name = drow["Name"].ToString(); 
String desc = drow["Description"].ToString(); 
DateTime dt = Convert.ToDateTime(drow["PostDate"].ToString()); 
String postdate = dt.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); 
String mailid = drow["Email"].ToString(); 
0

有作爲答案的一個建議

更換String sql = "select * from Forumthread where PostID=" + postid;

String sql = "select * from Forumthread where PostID='" + postid + "'";

或U嘗試以下操作:

如果它不起作用,那麼可能你將不得不直接在查詢中使用int。 爲此,只需在整型變量中獲取您的queryString值即可。 像:

int postid = Convert.ToInt32(Request.QueryString["id"]); 

String sql = "select * from Forumthread where PostID=" + postid;