4

I have the following SqlDataSource

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>" 
        InsertCommand="INSERT INTO [Chat] ([Username], [Message]) VALUES (@Username, @Message)" 
        SelectCommand="SELECT [Id], [Username], [Message], [Date] FROM [Chat] ORDER BY [Id]" >
        <InsertParameters>
            <asp:Parameter Name="Message" Type="String" />
            <asp:Parameter Name="Date" Type="DateTime" /> 
            <asp:Parameter Name="Username" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

Which controls the following FormView:

<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert"
        OnItemInserted="fv_ItemInserted" RenderOuterTable="False" 
            DataSourceID="SqlDataSource1">
        <InsertItemTemplate>
            <asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
            <asp:TextBox ID="TextBox1" runat="server" CssClass="chattxtbox"
            Text='<%# Bind("Message") %>' autocomplete="off"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" CommandName="insert" style="display:none" Text="Button" OnClick="insertUser"/>
            </asp:Panel>
        </InsertItemTemplate>
        </asp:FormView>

I want to be able a variable's content into the Username column, so on Button1 I set the following event: OnClick="insertUser"

 protected void insertUser(object sender, EventArgs e)
    {
        string username1 = User.Identity.Name;
        SqlDataSource1.InsertParameters.Add("Username", username1);
        SqlDataSource1.Insert();
    }

This isn't working though, I don't get any SQL error message at all, is this the right way to do this? And where did I go wrong?

3
  • Are Message and Date required columns in your database ? Commented Aug 23, 2012 at 14:26
  • All columns are required Commented Aug 23, 2012 at 14:39
  • Can you try to add message parameter and see if it inserts ? Commented Aug 23, 2012 at 14:40

2 Answers 2

3

Change Insert Parameter to SessionParameter.

<InsertParameters>
   <asp:Parameter Name="Message" Type="String" />
   <asp:Parameter Name="Date" Type="DateTime" /> 
   <asp:Parameter Name="Username" Type="String" />
   <asp:SessionParameter Name="Username" SessionField="Username" Type="String" />
</InsertParameters>

And in Page_Load handler,

Session["Username"]=User.Identity.Name;
Sign up to request clarification or add additional context in comments.

3 Comments

This sounds good, but still I get the same result... nothing happens. I thought this could be something on my FormView and pressing Enter maybe not triggering, but I just tried inserting from Textboxes and it all works fine.
Does it throws an exception? I think you should remove Date parameter because of this SQL statement - INSERT INTO [Chat] ([Username], [Message]) VALUES (@Username, @Message)
OK, seems like I was having some localhost conflict, this solution works very well, cheers man.
0

Here you want to declare your parameter as a Control Parameter and then you don't need to assign the value to the parameter in code. You can just call insert.

1 Comment

How would I assign User.Identity.Name as a value though?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.