Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
Are Message and Date required columns in your database ? – Pit Digger Aug 23 '12 at 14:26
All columns are required – Joao Ferreira Aug 23 '12 at 14:39
Can you try to add message parameter and see if it inserts ? – Pit Digger Aug 23 '12 at 14:40

2 Answers

up vote 2 down vote accepted

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;
share|improve this answer
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. – Joao Ferreira Aug 23 '12 at 14:50
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) – AVD Aug 23 '12 at 14:53
1  
OK, seems like I was having some localhost conflict, this solution works very well, cheers man. – Joao Ferreira Aug 23 '12 at 14:55

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.

share|improve this answer
How would I assign User.Identity.Name as a value though? – Joao Ferreira Aug 23 '12 at 14:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.