代码之家  ›  专栏  ›  技术社区  ›  prakash

AjAX分级控制使用asp.net c#i中的中继器存储和获取数据库

  •  0
  • prakash  · 技术社区  · 9 年前

    我收到错误:

    指定的强制转换无效。

    我需要重复每个已经从数据库中评级明星的用户如何从数据库中获得评级。这里是如何从数据库中获取评级。

    以下是存储过程:

    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>
    

    这是页面的C#后面的代码

     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;
            }
        }
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Jignesh Khokhariya    9 年前

    试试这个,

            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列所在的列号。