Table Name "A"
| ID | UserName | Password |
| 1 | xxx | iii |
| 2 | yyy | iii |
Table Name "B"
| ID | Post_Data | Created_By | Created_At |
| NULL | hi | NULL | NULL |
| NULL | GM | NULL | NULL |
This is my two tables when i am login the ID have to pass to second table ...But I am getting NULL Value What to do? PLEASE HELP
This is My code:
SAMPLE.aspx.cs
protected void btnlogin_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from TBL_Login where UserName =@UserName and Password=@Password", con);
cmd.Parameters.AddWithValue("@UserName", txtlogin.Text);
cmd.Parameters.AddWithValue("@Password", txtpassword.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Session["UserName"] = Convert.ToString(txtlogin.Text);
if (dt.Rows.Count > 0)
{
Response.Redirect("AthenaTechnologySolutions.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "");
}
}
Example.aspx.cs
public partial class AthenaTechnologySolutions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblsession.Text = "Welome " + Convert.ToString(Session["UserName"]);
if (!IsPostBack)
{
Update_Status();
}
}
protected void btnlogout_Click(object sender, EventArgs e)
{
Response.Redirect("LoginForm.aspx");
}
protected void btnpost_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
string query = "INSERT INTO TBL_Post (Post_data) VALUES (@Post_data)";
try
{
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@Post_data", txtpost.Text);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
public DataTable Update_Status()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
//SqlDataAdapter Adp = new SqlDataAdapter("select * from TBL_Post", con);
SqlCommand cmd = new SqlCommand("Proc_Login", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter Adp = new SqlDataAdapter();
Adp.SelectCommand = cmd;
DataTable Dt = new DataTable();
Adp.Fill(Dt);
statusgrdview.DataSource = Dt;
statusgrdview.DataBind();
return Dt;
}

Vishwakant TripathiPosted Apr 29, 2015, 4:50 AM
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
string query = "INSERT INTO TBL_Post (ID,Post_data) VALUES (@ID,@Post_data)";
try
{
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@Post_data", Convert.Int32(Session["ID"]));
cmd.Parameters.Add("@Post_data", txtpost.Text);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
Deepak RatanPosted Apr 29, 2015, 6:35 AM
Vishwakant TripathiPosted Apr 29, 2015, 6:00 AM
Let's Make it simple, first create a sample application with repeater and when you be comfortable with it , try to implement in your scenario.
Deepak RatanPosted Apr 29, 2015, 5:40 AM
I tryed dynamic gridview
Just it will display all datas what are all in Table Right?
In dynamic gridview we con't insert a like button and command button for each status
For these Issues i think that we can use GRIDVIEW
Its possible different user can use new GRIDVIEW to update the status?
Vishwakant TripathiPosted Apr 29, 2015, 5:19 AM
You can customize your grid view, but that will be more costly, and need more effort than repeater.
Deepak RatanPosted Apr 29, 2015, 5:06 AM
Actually when different user login Post there status
Its updating in same GRIDVIEW
It should display in new Gridview like facebook
If the same user post another status it should display in new GridView
can you give the solution? For this GRIDVIEW
Deepak RatanPosted Apr 29, 2015, 4:59 AM
Sneha SnehaPosted Apr 29, 2015, 4:39 AM
make your identity column i.e. ID as foreign key also.
then when you are inserting the data into your first table i.e. Table A also make query of insertion in the procedure to insert that same ID into another table i.e. Table B.
Insert into A (Username,Password) values (@Username, @Password);
Insert into B (ID) values (scope_identity());
remember scope_identity is a function
Regards
Sneha
Vishwakant TripathiPosted Apr 29, 2015, 3:23 AM
SqlCommand cmd = new SqlCommand("select * from TBL_Login where UserName =@UserName and Password=@Password", con);
Later, you are filling datatable, dt has all data of user. and you can fetch Id value from dt as below.
if (dt.Rows.Count>0)
{
Session["Id"] = dt.Rows[0]["Id"];
}
And Session can be used on any page once it is generated, so no need to worry about how to pass session["Id"], wherever, on any page, you can access it.
Deepak RatanPosted Apr 29, 2015, 2:50 AM
whos login
can you give some idea how to create a session for ID to pass to second table,Whos login?
Vishwakant TripathiPosted Apr 29, 2015, 2:39 AM
string query = "INSERT INTO TBL_Post (ID, Post_data) VALUES (@ID,@Post_data)";
here you only need to pass Session["Id"] value in @Id.
It is same for SP also, any how you need to pass Id in SP also as parameter.
So I think your problem is only to get Id while user logged in, there you are making Session["UserName"], at same place you just need to make another session for Id also, and that Session value you can fetch in Example.aspx.cs file.
Give a try.
Deepak RatanPosted Apr 29, 2015, 2:33 AM
I dont no how to do..!!
Deepak RatanPosted Apr 29, 2015, 2:31 AM
But in Table B ID i am not inserting any value
When the user login he will post any information in textbox by button click the information will store in the database, According to user login the ID have to set in second table and post information
But I am getting only post information , ID is in NULL value,
How to write SP? Please Help
Vishwakant TripathiPosted Apr 29, 2015, 2:16 AM
string query = "INSERT INTO TBL_Post (Post_data) VALUES (@Post_data)";
So i think you need to modify your insert query..
string query = "INSERT INTO TBL_Post (ID, Post_data) VALUES (@ID,@Post_data)";
You need to create a Session[ID] when user logged in and fetch it from database that you are doing already in btnlogin_Click
SqlCommand cmd = new SqlCommand("select * from TBL_Login where UserName =@UserName and Password=@Password", con);
and that Session[Id] value, you can pass into insert query.
I hope it would work for you.
Rahul BansalPosted Apr 29, 2015, 2:11 AM