2012-05-27 111 views
0

我在ASP.Net中構建了一個論壇,但有一個小問題。數據未寫入數據庫ASP.Net

我有1個用戶創建了一個主題,並且可以向其發送帖子,但是如果其他用戶登錄,它不會將帖子插入到數據庫中。它回來了,但沒有插入。原來的用戶可以登錄,但仍然可以發佈,但沒有其他人可以。

這是我在背後

protected void addPostBtn_Click(object sender, EventArgs e) 
{   
    //Define ADO.NET objects. 
    string insertSQL; 
    string topic = Request.QueryString["topicid"].ToString(); 
    insertSQL = "INSERT INTO Posts (TopicID, PostBody, PUserID)" 
     + "VALUES (@Topic, @NewPostText, @PUserID)"; 

    SqlConnection con = new SqlConnection(connectionString); 
    SqlCommand cmd = new SqlCommand(insertSQL, con); 

    // Try to open the database and execute the update 
    int added = 0; 
    try 
    { 
     cmd.Parameters.AddWithValue("@Topic", topic); 
     cmd.Parameters.AddWithValue("@NewPostText", newPostText.InnerText); 
     cmd.Parameters.AddWithValue("@PUserID", Session["User_ID"]); 
     con.Open(); 
     added = cmd.ExecuteNonQuery(); 
     lblResults.Text = "Your post has been added"; 
    } 
    catch (Exception err) 
    { 
     lblResults.Text = "Error inserting record. " + err.Message; 
    } 
    finally 
    { 
     con.Close(); 
    } 
    if (added > 0) 
    { 
     this.BindRepeater(); 
    } 
} 

代碼,我完全不出現任何錯誤。它說它提交的很好,但它不在數據庫中,除非原始的海報做到了。

編輯:

剛剛意識到這是我的觀點做。這是我目前認爲它是從

SELECT dbo.Posts.PostBody, dbo.Posts.PostDate, dbo.Posts.PostID, dbo.[User].username, dbo.Topic.TopicID 
FROM dbo.Topic RIGHT OUTER JOIN 
dbo.Posts ON dbo.Topic.TopicID = dbo.Posts.TopicID LEFT OUTER JOIN 
dbo.[User] ON dbo.Topic.TUserID = dbo.[User].UserID AND dbo.Posts.PUserID = dbo.[User].UserID 

讀書但現在已經返回NULL的其他用戶名

+0

那麼你的帖子表格的樣子?這是創建帖子時主題創建者使用的相同代碼嗎? – peroija

+0

@peroija請參閱編輯 – dpDesignz

+0

好吧,你想用這個視圖做什麼?爲什麼右外連接開始而不是離開外連接? – peroija

回答

0

的觀點必須如下

SELECT  dbo.Posts.PostBody, dbo.Posts.PostDate, dbo.Posts.PostID, dbo.[User].username, dbo.Topic.TopicID 
FROM   dbo.Topic LEFT OUTER JOIN 
        dbo.Posts ON dbo.Topic.TopicID = dbo.Posts.TopicID LEFT OUTER JOIN 
        dbo.[User] ON dbo.Posts.PUserID = dbo.[User].UserID