Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am creating an online news website. The user needs to login first so that they can add their news article. If the user logged in and create their own articles, how can I get their id and save it in the database along with their news article?

What I have tried:

//This is my code in adding news articles

public partial class Articles : System.Web.UI.Page
{
    string cs = ConfigurationManager.ConnectionStrings["NewsDataBaseConnectionString2"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCategory(); 
        }
    }
    protected void btn_AddArticles_Click(object sender, EventArgs e)
    {

        SqlConnection cn = new SqlConnection(cs);
        cn.Open();
        SqlCommand cm = new SqlCommand("Insert into tbl_AddNews (User_id,title, category_id, details, photo, date) values(@Userid, @Title, @Category, @Details, @photo, @Date)", cn);
        cm.Parameters.AddWithValue("@Title", txtbox_Title.Text);
        cm.Parameters.AddWithValue("@Category", DropDownList1.SelectedValue);
        cm.Parameters.AddWithValue("@Details", txtbox_details.Text);
        string strImg = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
        cm.Parameters.AddWithValue("@photo", strImg);
        cm.Parameters.AddWithValue("@Date", DateTime.Now.ToString());
        cm.ExecuteNonQuery();
        cn.Close();
        FileUpload1.PostedFile.SaveAs(Server.MapPath("Images\\") + strImg);

        
        Response.Redirect("Articles.aspx");
    }

    private void LoadCategory()
    {
        try
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("select * from tbl_Category", con);
                con.Open();
                DataTable table = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                adapter.Fill(table);

                DropDownList1.DataSource = table;
                DropDownList1.DataValueField = "Category_id";
                DropDownList1.DataTextField = "Category_Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem("--Select Category--", "0"));
            }
        }

        catch (Exception ex)
        {
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "Something went wrong!." + ex.Message + "";
        }
    }
}
Posted
Updated 2-Oct-17 17:25pm
Comments
Karthik_Mahalingam 28-Sep-17 5:21am    
what kind of authentication you are using?
Member 13432601 28-Sep-17 10:16am    
I'm just a begginer
Please explain the authentication.
Karthik_Mahalingam 28-Sep-17 10:35am    
Then from where you will get the id?
Member 13432601 28-Sep-17 20:05pm    
I have 3 tables in my database tbl_User, tbl_Category, tbl_NewsArticles
I used session to open the account of user.
Karthik_Mahalingam 28-Sep-17 23:21pm    
how does the user logs in to the application ?

1 solution

As per comments you are storing the user id in the session, so you shall get the id from session.
C#
string userID = Session["New"]


Note: Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900