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 TripathiRank #1,870 · 797 reputationPosted Apr 29, 2015, 12:50 AM EDT
{
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());
}
}
Vishwakant TripathiRank #1,870 · 797 reputationPosted Apr 28, 2015, 10:39 PM EDT
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.
Vishwakant TripathiRank #1,870 · 797 reputationPosted Apr 28, 2015, 11:23 PM EDT
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.
Vishwakant TripathiRank #1,870 · 797 reputationPosted Apr 29, 2015, 2:00 AM EDT
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.
Rahul BansalRank #142 · 15.9K reputationPosted Apr 28, 2015, 10:11 PM EDT
Vishwakant TripathiRank #1,870 · 797 reputationPosted Apr 28, 2015, 10:16 PM EDT
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.
Deepak Ratan175 reputationPosted Apr 28, 2015, 10:31 PM EDT
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
Deepak Ratan175 reputationPosted Apr 28, 2015, 10:33 PM EDT
I dont no how to do..!!
Deepak Ratan175 reputationPosted Apr 28, 2015, 10:50 PM EDT
whos login
can you give some idea how to create a session for ID to pass to second table,Whos login?
Sneha Sneha77 reputationPosted Apr 29, 2015, 12:39 AM EDT
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
Deepak Ratan175 reputationPosted Apr 29, 2015, 12:59 AM EDT
Deepak Ratan175 reputationPosted Apr 29, 2015, 1:06 AM EDT
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
Vishwakant TripathiRank #1,870 · 797 reputationPosted Apr 29, 2015, 1:19 AM EDT
You can customize your grid view, but that will be more costly, and need more effort than repeater.
Deepak Ratan175 reputationPosted Apr 29, 2015, 1:40 AM EDT
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?
Deepak Ratan175 reputationPosted Apr 29, 2015, 2:35 AM EDT