I'm having a problem using ASP and C# i have a form with 2 dropdownlist one shows topic and the other shows the subtopic, now i added a StoredProcedure that checks if user has permission to view the topic if so it will be added to the dropdownlist, and it works fine but when i select a topic it just returns to the first option always, anyone know what i can do ? i've read about Postback because of the bind put i dont seem to get it to work here is my code... ive also tried using sqladapter same issue tho ....
stored Procedure
USE [Forum]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_verutilizadorTopico]
(
@id_user int
)
AS
SELECT UtilizadorTopico.IdTopico, Topico.Id_topico, Topico.Nome FROM UtilizadorTopico INNER JOIN Topico ON UtilizadorTopico.IdTopico = Topico.Id_topico WHERE (UtilizadorTopico.IdUtilizador = @id_user )
RETURN
and my C# code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
public partial class About : System.Web.UI.Page
{
string loginid = "";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString());
public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
topico1();
BindRepeaterData();
BindRepeaterDataPost();
}
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString()); //definição do comando sql
conn.Open();
//Cria uma objeto do tipo comando passando como parametro a string sql e a string de conexão
//Adicionando o valor das textBox nos parametros do comando
SqlCommand comm = conn.CreateCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = "SP_veruseraposlogin";
comm.Parameters.AddWithValue("@nome", Page.User.Identity.Name);
SqlDataReader rdr = comm.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
loginid = rdr["Id"].ToString();
}
Lblidlog.Text = loginid;
conn.Close();
}
}
public void topico1()
{
SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString()); //definição do comando sql
conn2.Open();
//Cria uma objeto do tipo comando passando como parametro a string sql e a string de conexão
//Adicionando o valor das textBox nos parametros do comando
SqlCommand comm2 = conn2.CreateCommand();
comm2.CommandType = CommandType.StoredProcedure;
comm2.CommandText = "SP_verutilizadorTopico";
comm2.Parameters.AddWithValue("@id_user", loginid);
SqlDataReader rdrtop = comm2.ExecuteReader();
droptopico.DataSource = rdrtop;
droptopico.DataTextField = "Nome";
droptopico.DataValueField = "Id_topico";
droptopico.DataBind();
rdrtop.Close();
conn2.Close();
}
protected void BindRepeaterData()
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Mensagem where id_subtopico = @id_sub Order By data desc", conn);
DataSet ds = new DataSet();
if (dropsub.SelectedValue != "")
{
cmd.Parameters.AddWithValue("@id_sub", this.dropsub.SelectedValue);
}
else
{
cmd.Parameters.AddWithValue("@id_sub", 1);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepDetails.DataSource = ds;
RepDetails.DataBind();
conn.Close();
}
protected void DropsubSelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString()); //definição do comando sql
conn2.Open();
SqlCommand comm2 = conn2.CreateCommand();
comm2.CommandType = CommandType.StoredProcedure;
comm2.CommandText = "SP_verutilizadorTopico";
comm2.Parameters.AddWithValue("@id_user", loginid);
comm2.Parameters.AddWithValue("@id_topico", droptopico.SelectedValue);
BindRepeaterData();
BindRepeaterDataPost();
}
protected void BindRepeaterDataPost()
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT Post.*, Ficheiro.Nome FROM Ficheiro INNER JOIN Post ON Ficheiro.IdFicheiro = Post.IdFicheiro where Post.id_subtopico = @Id_sub Order By data desc", conn);
DataSet ds = new DataSet();
if (dropsub.SelectedValue != "")
{
cmd.Parameters.AddWithValue("@id_sub", this.dropsub.SelectedValue);
}
else
{
cmd.Parameters.AddWithValue("@id_sub", 1);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
RepDetailsPost.DataSource = ds;
RepDetailsPost.DataBind();
conn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
//definição da string de conexão
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString2"].ToString()); //definição do comando sql
conn.Open();
SqlCommand sqlCommand = conn.CreateCommand();
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "usp_inserirmsg";
sqlCommand.Parameters.AddWithValue("@id_sub", this.dropsub.SelectedValue);
sqlCommand.Parameters.AddWithValue("@nome_user", Page.User.Identity.Name);
sqlCommand.Parameters.AddWithValue("@msg", this.txtmsg.Text);
sqlCommand.ExecuteNonQuery();
this.lblstatusmsg.ForeColor = Color.FromArgb(255, 6, 255, 196);
lblstatusmsg.Text = "Mensagem enviada";
conn.Close();
}
protected void novocomentario_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Panel1.Visible = true;
}
}
Topico1() is where bind to the dropdownlist.