.cs page code

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class advertisementSubmission : System.Web.UI.Page
{
    protected String EmplyrSession;
    protected int userId;

    protected void Page_Load(object sender, EventArgs e)
    {


        EmplyrSession = (String)Session["UserEmail"];
        Response.Write(EmplyrSession);
        String conString = "Data Source=COSANOSTRA;Initial Catalog=Waleed_orsfinal;Integrated Security=True";
        //Sql Connection
        SqlConnection con = new SqlConnection(conString);
        //Sql Command
        SqlCommand com = new SqlCommand("advSubmissionUserid", con);
        com.CommandType = CommandType.StoredProcedure;


        com.Parameters.Add("@sessionVar", SqlDbType.VarChar).Value = EmplyrSession;
        com.Parameters.Add("@ReturnValueUserId", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;

        try
        {
            con.Open();
            com.ExecuteNonQuery();
            userId = (int)com.Parameters["@ReturnValueUserId"].Value;
            Response.Write(userId);

            SqlDataSource1.InsertParameters["@userId"].DefaultValue = userId.ToString();

        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.Message);
        }
        finally
        {
            con.Close();
        }


    }

}

.aspx page code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="advertisementSubmission.aspx.cs" Inherits="advertisementSubmission" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Submit JobDetails</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DetailsView ID="DetailsView1" runat="server" AccessKey="d" 
            AutoGenerateRows="False" DataKeyNames="advid" DataSourceID="SqlDataSource1" 
            DefaultMode="Insert" Height="50px" Width="125px" CellPadding="4" 
            ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <Fields> 
                <asp:BoundField DataField="advid" HeaderText="advid" InsertVisible="False" 
                    ReadOnly="True" SortExpression="advid" />
                <asp:BoundField DataField="advtitle" HeaderText="advtitle" 
                    SortExpression="advtitle" />
                <asp:BoundField DataField="startingdate" HeaderText="startingdate" 
                    SortExpression="startingdate" />
                <asp:BoundField DataField="endingdate" HeaderText="endingdate" 
                    SortExpression="endingdate" />
                <asp:BoundField DataField="mediumofadv" HeaderText="mediumofadv" 
                    SortExpression="mediumofadv" />
                <asp:BoundField DataField="refid" HeaderText="refid" SortExpression="refid" />
                <asp:BoundField DataField="jbid" HeaderText="jbid" SortExpression="jbid" ReadOnly="True" Visible="false" />
                 <asp:TemplateField HeaderText="advDetails" SortExpression="advDetails">
                  <EditItemTemplate>
                   <asp:TextBox ID="txtboxAdvDetails" runat="server" Text='<%#Bind("advDetails") %>' TextMode="MultiLine"></asp:TextBox>
                  </EditItemTemplate>
                 </asp:TemplateField>
                <asp:BoundField DataField="userId" HeaderText="userId" SortExpression="userId" Visible="false" />
                <asp:CommandField ShowInsertButton="True"  />
            </Fields>
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Waleed_orsfinalConnectionString %>" 
            DeleteCommand="DELETE FROM [tblJobAdv] WHERE [advid] = @advid" 
            InsertCommand="INSERT INTO [tblJobAdv] ([advtitle], [startingdate], [endingdate], [mediumofadv], [refid], [jbid], [advDetails], [userId]) VALUES (@advtitle, @startingdate, @endingdate, @mediumofadv, @refid, @jbid, @advDetails, @userId)" 
            SelectCommand="SELECT * FROM [tblJobAdv]" 
            UpdateCommand="UPDATE [tblJobAdv] SET [advtitle] = @advtitle, [startingdate] = @startingdate, [endingdate] = @endingdate, [mediumofadv] = @mediumofadv, [refid] = @refid, [jbid] = @jbid WHERE [advid] = @advid">
            <DeleteParameters>
                <asp:Parameter Name="advid" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="advtitle" Type="String" />
                <asp:Parameter DbType="Date" Name="startingdate" />
                <asp:Parameter DbType="Date" Name="endingdate" />
                <asp:Parameter Name="mediumofadv" Type="String" />
                <asp:Parameter Name="refid" Type="Int32" />
                <asp:Parameter Name="jbid" Type="Int32" />
                <asp:Parameter Name="advid" Type="Int32" />

            </UpdateParameters>

            <InsertParameters>
                <asp:Parameter Name="advtitle" Type="String" />
                <asp:Parameter DbType="Date" Name="startingdate" />
                <asp:Parameter DbType="Date" Name="endingdate" />
                <asp:Parameter Name="mediumofadv" Type="String" />
                <asp:Parameter Name="refid" Type="Int32" />          
                <asp:QueryStringParameter Name="jbid" QueryStringField="jbid" Type="Int32" />
                <asp:Parameter Name="advDetails" Type="String" />
                <asp:Parameter Name="userId" Type="Int32"/>


            </InsertParameters>
        </asp:SqlDataSource>

    </div>

    </form>
</body>
</html>

i get error , **Object reference not set to an instance of an object. **

ok guys, its done, just remove @ from userid ,
44.SqlDataSource1.InsertParameters["@userId"].DefaultValue = userId.ToString();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.