2015-02-12 47 views
0

我得到錯誤:AJAX等級控制存儲和asp.net C#中使用中繼器從數據庫中獲取我

Specified cast is not valid.

我需要重複每一個用戶已經從數據庫星級評分:如何從數據庫獲取評級。這裏如何從數據庫中獲取評分。

這裏是存儲過程:

ALTER PROCEDURE [dbo].[sp_comments] 
(
@eid int 
) 
AS 
BEGIN 
    declare @sql varchar(max) 
    SET NOCOUNT ON; 

    select @sql='select g.username,dbo.fn_username(g.updation) as updation, 
    c.comment_id,c.id,c.comments,c.commented_by,c.mail,c.date,c.rating_star 
    from comments c 
    inner join tbl_data as g on g.id=c.id 
    WHERE c.id=''' +CONVERT(VARCHAR(50),@eid) +''' order by comment_id desc' 
    exec(@sql) 
    print(@sql) 

END 

這裏是我的asp.net頁面設計:

<asp:Rating ID="user_rating" runat="server" CurrentRating='<%#Eval("rating_star")%>' RatingDirection="LeftToRightTopToBottom" StarCssClass="ratingStar" WaitingStarCssClass="SavedRatingStar" FilledStarCssClass="FilledRatingStar" 
EmptyStarCssClass="EmptyRatingStar" AutoPostBack="true"></asp:Rating> 

這裏是後面的代碼 - 網頁

protected void Button1_Click(object sender, EventArgs e) 

    { 
     if (Request.QueryString["id"] != null) 
     { 
     int id; 
     id = Convert.ToInt32(Request.QueryString["id"].ToString()); 

     con = new SqlConnection(str); 
     con.Open(); 
     cmd = new SqlCommand("sp_usercomment", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@mail", mail_by.Text); 
     cmd.Parameters.AddWithValue("@comments", comments.Text); 
     cmd.Parameters.AddWithValue("@name", name_by.Text); 
     cmd.Parameters.AddWithValue("@updated_name", Convert.ToInt32(Request.Cookies["userid"].Value)); 
     cmd.Parameters.AddWithValue("@eid",id); 
     cmd.Parameters.AddWithValue("@rating",SqlDbType.Int).Value=rating.CurrentRating; 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     Label2.Visible = true; 
     Label2.Text = "Thanks for your valuable feedback"; 

     mail_by.Text = ""; 
     comments.Text = ""; 
     name_by.Text= ""; 


     getcomments(); 
     } 
    } 


void getcomments() 

    { 
     con = new SqlConnection(str); 
     con.Open(); 
     cmd = new SqlCommand("sp_comments", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.Add(new SqlParameter("@eid", Request.QueryString["id"].ToString())); 
     da=new SqlDataAdapter(cmd); 
     dt = new DataTable(); 
     da.Fill(dt); 
     con.Close(); 

     comment_label.Text = "View " + dt.Rows.Count.ToString() + " Comments"; 
     if (dt.Rows.Count > 0) 
     { 
      DataRow dr=dt.Rows[0]; 
      repeat.DataSource = dt.DefaultView; 
      repeat.DataBind(); 
      review_panel.Visible = true; 
      product = dr["username"].ToString(); 


     } 
     else 
     { 
      review_panel.Visible = false; 
     } 
    } 
+0

所以你的asp:評級標籤在中繼器的權利?比「dt」給重複的數據源....即, repeat.DataSource = DT; – 2015-02-12 08:56:46

+0

我改變了,但同樣的錯誤... – prakash 2015-02-12 09:25:13

+0

如何從數據庫中得到評級給查詢.... – prakash 2015-02-12 09:25:54

回答

0

的C#試試這個,

 con = new SqlConnection(str); 
     con.Open(); 
     cmd = new SqlCommand("sp_comments", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.Add(new SqlParameter("@eid",Request.QueryString["id"].ToString())); 
     da=new SqlDataAdapter(cmd); 
     dt = new DataTable(); 
     da.Fill(dt); 
     con.Close(); 

     DataTable dtCloned = dt.Clone(); 
     dtCloned.Columns[3].DataType = typeof(Int32); 
     foreach (DataRow row in dt.Rows) 
     { 
      dtCloned.ImportRow(row); 
     } 
     repeat.DataSource = dtCloned; 
     repeat.DataBind(); 

注意:dtCloned.Columns [3]。中的「3」.DataType將是您的列號,其中rating_star列將進入您的數據表中。

+0

您是否同時聲明瞭兩個數據表? – prakash 2015-02-12 10:36:24

+0

,因爲您的數據表「dt」將存儲字符串數據類型中的所有數據。所以我們希望將「rating_star」數據保存爲整數,所以我將該列轉換爲整數。 – 2015-02-12 10:40:23

+0

雅我得到了輸出......謝謝..如果你有答案可以將int轉換爲其他方式 – prakash 2015-02-12 10:49:32

相關問題