can't really give you a 100% accurate one becasue i'm not sure what all columns you have and what table you want. I see you have the news table but i'm assuming comments will be elsewehre with a newsid column
But advice. Look into using somethign other than on screen objects for data communication. Using the DataSource objects are great for first learnign how to write code, but quickly look into using like Entity Framework and binding from codebehind etc and not
using the drag and drop objects as that has no seperation of data layers like UI and Data etc.
Coding is like life, learn from the past and work towards the future, trying to redo past code, ends in lost time and nothing new accomplished.
The dbs name is Dragons_news_comments and the colums is UserName where it takes "User.Identity.Isauthenticated" from codebehind but I just have to figure out how to retrieve that and the next column is CommentMsg and the last is NewsID
Well you need to add the third parameter you talkeda bout with the user identity . You said you figured that out right? To execute it on button push you should be able to do sqldata_rpt_news.Insert();
Coding is like life, learn from the past and work towards the future, trying to redo past code, ends in lost time and nothing new accomplished.
romedk
Member
1 Points
15 Posts
Sqldatasource querystring
Jul 09, 2012 01:06 PM|LINK
I want to make comments on my news post and I've used querystring for them so I can have 1 post for each page like news.aspx?ID=(ID number)
But how do I retrieve the current ID in the querystring to insert into the db
<asp:Repeater ID="rpt_news" runat="server" DataSourceID="sqldata_rpt_news"> <ItemTemplate> <div class="news"> <div class="news_head"> <%#Eval ("NewsHead") %> <div style="float: right; font-size: 12px; margin-top: 5px;"> Written by <%#Eval ("NewsAuthor") %> <%#Eval("NewsDate", "{0:dd/MM/yyyy}")%></div> </div> <div class="news_box"> <%#Eval ("NewsTxt") %> </div> </div> </ItemTemplate> </asp:Repeater> <asp:SqlDataSource ID="sqldata_rpt_news" runat="server" ConnectionString="<%$ ConnectionStrings:deanportfolio_dk_dbConnectionString %>" SelectCommand="SELECT * FROM [Dragons_news] WHERE ([NewsID] = @NewsID)"> <SelectParameters> <asp:QueryStringParameter DefaultValue="2" Name="NewsID" QueryStringField="ID" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>AWAlger
Contributor
2531 Points
540 Posts
Re: Sqldatasource querystring
Jul 09, 2012 01:58 PM|LINK
Same way you do to get the id if your using the sqldatasource. You create a insertcommand with InsertParameters.
Coding is like life, learn from the past and work towards the future, trying to redo past code, ends in lost time and nothing new accomplished.
Don't forget to mark Answer!!!!!!!
romedk
Member
1 Points
15 Posts
Re: Sqldatasource querystring
Jul 09, 2012 03:06 PM|LINK
Can you please show me abit code for it?
I've only been doing C# for 2 weeks but can do alot already
AWAlger
Contributor
2531 Points
540 Posts
Re: Sqldatasource querystring
Jul 09, 2012 03:18 PM|LINK
can't really give you a 100% accurate one becasue i'm not sure what all columns you have and what table you want. I see you have the news table but i'm assuming comments will be elsewehre with a newsid column
<asp:SqlDataSource ID="sqldata_rpt_news" runat="server" ConnectionString="<%$ ConnectionStrings:deanportfolio_dk_dbConnectionString %>" SelectCommand="SELECT * FROM [Dragons_news] WHERE ([NewsID] = @NewsID)"> <SelectParameters> <asp:QueryStringParameter DefaultValue="2" Name="NewsID" QueryStringField="ID" Type="Int32" /> </SelectParameters> InsertCommand="INSERT INTO Dragons_news_comments (comments, newsID) VALUES (@comments,@newsID)"> <insertparameters> <asp:formparameter name="comments" formfield="txtComments" /> <asp:QueryStringParameter DefaultValue="2" Name="NewsID" QueryStringField="ID" Type="Int32" /> </insertparameters> </asp:SqlDataSource>But advice. Look into using somethign other than on screen objects for data communication. Using the DataSource objects are great for first learnign how to write code, but quickly look into using like Entity Framework and binding from codebehind etc and not using the drag and drop objects as that has no seperation of data layers like UI and Data etc.
Coding is like life, learn from the past and work towards the future, trying to redo past code, ends in lost time and nothing new accomplished.
Don't forget to mark Answer!!!!!!!
romedk
Member
1 Points
15 Posts
Re: Sqldatasource querystring
Jul 09, 2012 06:30 PM|LINK
The dbs name is Dragons_news_comments and the colums is UserName where it takes "User.Identity.Isauthenticated" from codebehind but I just have to figure out how to retrieve that and the next column is CommentMsg and the last is NewsID
AWAlger
Contributor
2531 Points
540 Posts
Re: Sqldatasource querystring
Jul 09, 2012 06:45 PM|LINK
Then add that to what i have.....
<asp:SqlDataSource ID="sqldata_rpt_news" runat="server" ConnectionString="<%$ ConnectionStrings:deanportfolio_dk_dbConnectionString %>" SelectCommand="SELECT * FROM [Dragons_news] WHERE ([NewsID] = @NewsID)"> <SelectParameters> <asp:QueryStringParameter DefaultValue="2" Name="NewsID" QueryStringField="ID" Type="Int32" /> </SelectParameters> InsertCommand="INSERT INTO Dragons_news_comments (UserName ,CommentMsg, NewsID) VALUES (@UserName ,@CommentMsg,@newsID)"> <insertparameters> <asp:formparameter name="CommentMsg" formfield="txtComments" /> <asp:formparameter name="UserName" formfield="UserName " /> <---- This is what you came up with for UserName ----> <asp:QueryStringParameter Name="NewsID" QueryStringField="ID" Type="Int32" /> </insertparameters> </asp:SqlDataSource>Coding is like life, learn from the past and work towards the future, trying to redo past code, ends in lost time and nothing new accomplished.
Don't forget to mark Answer!!!!!!!
romedk
Member
1 Points
15 Posts
Re: Sqldatasource querystring
Jul 09, 2012 07:22 PM|LINK
But how do i set a certain button to insert these data?
romedk
Member
1 Points
15 Posts
Re: Sqldatasource querystring
Jul 09, 2012 07:25 PM|LINK
This is how it looks like now
<asp:TextBox ID="txtComments" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:SqlDataSource ID="sqldata_rpt_news" runat="server" ConnectionString="<%$ ConnectionStrings:deanportfolio_dk_dbConnectionString %>"
SelectCommand="SELECT * FROM [Dragons_news] WHERE ([NewsID] = @NewsID)" InsertCommand="INSERT INTO Dragons_news_comments (UserName, CommentMsg, NewsID) VALUES (<%= User.Identity.Name%>,@comments,@newsID)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="2" Name="NewsID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:FormParameter Name="comments" FormField="txtComments" />
<asp:QueryStringParameter DefaultValue="2" Name="NewsID" QueryStringField="ID" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
AWAlger
Contributor
2531 Points
540 Posts
Re: Sqldatasource querystring
Jul 09, 2012 09:02 PM|LINK
Well you need to add the third parameter you talkeda bout with the user identity . You said you figured that out right? To execute it on button push you should be able to do sqldata_rpt_news.Insert();
Coding is like life, learn from the past and work towards the future, trying to redo past code, ends in lost time and nothing new accomplished.
Don't forget to mark Answer!!!!!!!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Sqldatasource querystring
Jul 11, 2012 01:53 AM|LINK
Please confirm whether your current page's url has the queryparameter something like:
xxx.aspx?id=Number
And you've defined the QueryStringParamter right in the InsertParameters,and then call SqlDataSource.Insert()……
Reguards!