Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai All,i write this code for inserting user data if he is already existing user.but i need to get his name and current id using sessions ,after that i can send other details along with these two to my DB.how can i do this
on page load
// string name = Session["nameforlogin"].ToString();
           // string id = Session["id"].ToString();

on button click
 SqlConnection conn = new SqlConnection();
            conn.ConnectionString="Data Source=USER-PC\\ADITYA;Initial Catalog=holder;Persist Security Info=True;User ID=sa;Password=123";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "insert into tb_userdata (employeeidfromtable1,Name,dicription,aboutme)  values ('" +( Session["id"]) + "','" + Session["name"] + "','" +txtdesr.Text + "','"+txturl .Text+"');";
            cmd.ExecuteNonQuery();
            conn.Close();

thanks and regards
Posted
Updated 17-Sep-13 9:36am
v2
Comments
Richard C Bishop 17-Sep-13 15:38pm    
You should not be doing any sql statements with concatenated strings of user input. It leaves you susceptible to SQL injection. Also, where do you supposed the name and id are coming from? When do they initially get stored in session?
adityaimmadi 17-Sep-13 15:46pm    
sorry for the above code , this is not the real one but it is actually a sample one like my original .the question you asked "When do they initially get stored in session?"is actually my problem.
string name = Session["nameforlogin"].ToString();
// string id = Session["id"].ToString();
if i write above 2 lines in page load it returns the error like "Object reference not set to an instance of an object".

can you please help me
Thanks and Regards
Richard C Bishop 17-Sep-13 15:49pm    
So that makes sense, you are assigning "name" and "id" values from session. If there is nothing in those session variables, you will get that error. When are you setting those session variables?
adityaimmadi 17-Sep-13 16:18pm    
sir,my scenario is registering user verifying againest database and inserting his personal details except his id and name (need to get these two from session ).i dont know how to bring name and id using sessions i tried above code then 'm getting errors like this.can you please suggest me how to overcome this
ZurdoDev 17-Sep-13 16:48pm    
You already have code, commented out, for using the session. What's the problem?

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