pass an ID with hyperlik but cant get this ID value from a fk in one table when i click in insert
- by susan
Something strange happened in my codes, actually I have a hyperlink that pass ID value in a query string to second page.in second page i have 2 sql datasource that both these sql datasources should get this id value and pass it to a filter parameter to show sth in datalist.
so in another word I have a first page that has an hyperlink read ID value from a datasource and pass it to second page.its like below:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "~/forumpage.aspx?ID="+Eval("ID")%>'><%#Eval("title")%> </asp:HyperLink>
then in second page i have one sql datasource with a query like this ...where ID=@id and get this id in query string from db.it work great . but i have problem with second sql datasource in second page it has a query sth like below:...forms.question_id=@id
then in sql reference both to query string as ID that get by first page in hyperlink.
but when i click in insert button show me error with fk.
error:Error:The INSERT statement conflicted with the FOREIGN KEY
constraint "FK_forumreply_forumquestions". The conflict occurred in
database "forum", table "dbo.forumquestions", column 'ID'. The
statement has been terminated.
my tables
(question(ID,user_id(fk),Cat_id(fk),title,bodytext)
(reply(ID,userr_id(fk),questionn_id(fk),titlereply,bodytestreply);
When by hand in cb i gave a number in questionn_id like 1 it show me successful but when it want read from a filter by datasource this field face with problem.
plzzzz help i really need skip from this part.and cause i am new i guess I cant understand the logic way clearly.
<asp:SqlDataSource ID="sdsreply" runat="server"
ConnectionString="<%$ ConnectionStrings:forumConnectionString %>"
SelectCommand="SELECT forumreply.ID, forumreply.userr_id, forumreply.questionn_id, forumreply.bodytextreply, forumreply.datetimereply, forumquestions.ID AS Expr1, forumusers.ID AS Expr2, forumusers.username FROM forumquestions INNER JOIN forumreply ON forumquestions.ID = forumreply.questionn_id INNER JOIN forumusers ON forumquestions.user_id = forumusers.ID AND forumreply.userr_id = forumusers.ID where forumreply.questionn_id=@questionn_id">
<SelectParameters>
<asp:QueryStringParameter Name="questionn_id" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
it is cb for second page in insert button:
{
if (Session["userid"] != null)
{
lblreply.Text = Session["userid"].ToString();
}
else
{
Session["userid"]=null;
}
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
lblshow.Text = string.Empty;
string d = HttpContext.Current.User.Identity.Name;
lblshow.Text =d + "???? ??? ?????." ;
foreach (DataListItem item in DataList2.Items)
{
Label questionn_idLabel = (Label)item.FindControl("questionn_idLabel");
Label userr_idLabel = (Label)item.FindControl("userr_idLabel");
lbltest.Text = string.Empty;
lbltest.Text = questionn_idLabel.Text;
lblreply.Text = string.Empty;
lblreply.Text = userr_idLabel.Text;
}
}
else
{
lblshow.Text = "??? ??? ??? ??? ?? ?? ?????? ???? ???? ???? ????? ??? ??? ? ??? ????? ???????.";
}
}
{
if(HttpContext.Current.User.Identity.IsAuthenticated)
{
if (Page.IsValid)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["forumConnectionString"].ConnectionString);
try
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into forumreply (userr_id,questionn_id,bodytextreply,datetimereply)values(@userr_id,@questionn_id,@bodytextreply,@datetimereply)", con);
cmd.Parameters.AddWithValue("userr_id",lblreply.Text);
cmd.Parameters.AddWithValue("questionn_id",lbltest.Text);
cmd.Parameters.AddWithValue("bodytextreply",txtbody.Text);
cmd.Parameters.AddWithValue("datetimereply",DateTime.Now );
cmd.ExecuteNonQuery();
}
catch (Exception exp)
{
Response.Write("<b>Error:</b>");
Response.Write(exp.Message);
}
finally
{
con.Close();
}
lblmsg.Text = "???? ??? ?? ?????? ??? ?????.thx";
lblshow.Visible = false;
//lbltxt.Text = txtbody.Text;
txtbody.Text = string.Empty;
}
}
else
{
lblmsg.Text = string.Empty;
Session["rem"] = Request.UrlReferrer.AbsoluteUri;
Response.Redirect("~/login.aspx");
}
}