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?

Jorg Ancrath
  • 1,447
  • 10
  • 34
  • 61

2 Answers2

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;
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 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. – Jorg Ancrath 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)` – KV Prajapati Aug 23 '12 at 14:53
  • 1
    OK, seems like I was having some localhost conflict, this solution works very well, cheers man. – Jorg Ancrath Aug 23 '12 at 14:55
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.

pwnyexpress
  • 1,016
  • 7
  • 14