how to insert data shown in datalist to a different table in the database C#

UrKo

Newcomer
Joined
May 12, 2009
Messages
11
hi,

first met me say i searched and read tutorials but i just cant make this to work on my site.

I have a datalist on the page where some data is shown. What i would like is to add a linkbutton so that registered users can add this data to its favorite list. I work with MS access database.

Code:
<asp:DataList ID="DataList1" runat="server" DataSourceID="PolnitevDetail">

<ItemTemplate>
<asp:Label class="Polnitev-podatki" ID="kroglaLabel" runat="server" Text='<%# Eval("PID") %>' />
<asp:Label class="Polnitev-podatki" ID="teza_krogleLabel1" runat="server" Text='<%# Eval("ID") %>'/>
<asp:Label class="Polnitev-podatki" ID="smodnikLabel" runat="server" Text='<%# Eval("UserId") %>' />
<asp:Label class="Polnitev-podatki" ID="smodnikLabel" runat="server" Text='<%# Eval("Nabojime") %>' />
<asp:Label class="Polnitev-podatki" ID="kroglaLabel" runat="server" Text='<%# Eval("krogla") %>' />
<asp:Label class="Polnitev-podatki" ID="teza_krogleLabel1" runat="server" Text='<%# Eval("teza_krogle") %>'/>
<asp:Label class="Polnitev-podatki" ID="smodnikLabel" runat="server" Text='<%# Eval("smodnik") %>' />

</ItemTemplate>
    </asp:DataList>
    <asp:AccessDataSource ID="PolnitevDetail" runat="server"
        DataFile="~/App_Data/Orozje-Reloading-Center.mdb" 
        SelectCommand="SELECT * FROM [POLNITVE_QUERY] WHERE ([PID] = ?)"
 InsertCommand="INSERT INTO MOJE_PRILJUBLJENE_POLNITVE(PID, ID, UserId, Nabojime, Krogla, teza_krogle, smodnik) VALUES (?, ?, ?, ?, ?, ?, ?)">

        <SelectParameters>
            <asp:QueryStringParameter Name="ID" QueryStringField="ID" Type="Int32" />
        </SelectParameters>
                <InsertParameters>
            <asp:Parameter Name="UserId" Type="Int32" />
            <asp:Parameter Name="ID" Type="Int32" />
            <asp:Parameter Name="PID" Type="Int32" />
            <asp:Parameter Name="Nabojime" Type="String" />
            <asp:Parameter Name="Krogla" Type="String" />
            <asp:Parameter Name="teza_krogle" Type="String" />
            <asp:Parameter Name="smodnik" Type="String" />
        </InsertParameters>
    </asp:AccessDataSource>

 <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>

and code behind

Code:
protected void LinkButton1_Click(object sender, EventArgs e)
  {
      PolnitevDetail.Insert();
  }
At the moment, when i click the LinkButton, i get empty result in database. All fields are empty.

Also...i would also like to write in the code behind a code that in case a user click LinkButton and if this data is already in his favorites, he would get a message that this data is already in his favorites.


----------

I also made another script but i get error all the time:
Code:
protected void LinkButton1_Click(object sender, EventArgs e)
  {
      string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Orozje-Reloading-Center.mdb";
      
      OleDbConnection conn = new OleDbConnection(connectionString);
      conn.Open();
      OleDbCommand cmd = new OleDbCommand();
      cmd.Connection = conn;
      cmd.CommandText = "INSERT INTO MOJE_PRILJUBLJENE_POLNITVE(PID, ID, UserId, Nabojime, Krogla, teza_krogle, smodnik) " + "VALUES  ('@PID','@ID','@UserId','@Nabojime','@Krogla','@teza_krogle','@smodnik')";

      cmd.CommandType = CommandType.Text;
      cmd.Parameters.Add("@PID", OleDbType.Integer, 8, "PID");
      cmd.Parameters.Add("@ID", OleDbType.Integer, 8, "ID");
      cmd.Parameters.Add("@UserId", OleDbType.Integer, 8, "UserId");
      cmd.Parameters.Add("Nabojime", OleDbType.VarChar, 255, "Nabojime");
      cmd.Parameters.Add("@Krogla", OleDbType.VarChar, 255, "Krogla");
      cmd.Parameters.Add("@teza_krogle", OleDbType.VarChar, 255, "teza_krogle");
      cmd.Parameters.Add("@smodnik", OleDbType.VarChar, 255, "smodnik");
      cmd.ExecuteNonQuery();
      conn.Close();
  }

 

Now i get 

Data type mismatch in criteria expression.


            

             Description: An
unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the
error and where it originated in the code. 



             Exception Details: System.Data.OleDb.OleDbException: Data type mismatch in criteria expression.



            Source Error: 



            

Line 36:       cmd.Parameters.Add("@teza_krogle", OleDbType.VarChar, 255, "teza_krogle");
Line 37:       cmd.Parameters.Add("@smodnik", OleDbType.VarChar, 255, "smodnik");
Line 38:       cmd.ExecuteNonQuery();
Line 39:       conn.Close();

Line 40:   }

As im a newbie any help would be great :)

thanks
 
Back
Top