On my Homepage i made i Visual Studio i want to have some welcome text on my firstpage.
The Thing is that i want in the middle of the text present some values that i grab from my db with a select query.
Lets say for example:
Welcome to my Homepage!
Currently there are (Select Count(UserID) FROM users where Status = 'active') Users active on my page and (Select Count(UserID) FROM users where Status = 'inactive') that are inactive.
Im really new to this but somehow i Need to run the my questions on Page_load and then be able to take that value and present it in a Label or something?
Thanks for your help
Using ADO.NET:
1: Create a connection object, set it's connection string property
2: Create a command object and set its text property to
Select Count(UserID) FROM users where Status = 'active'
3: Use ExecuteScalar method of the command object to find the result you want and set the label text property equal to that result.
Procedure
create procedure select
#activity
as
Begin
Select Count(UserID) FROM users where Status = #activity
end
C# Page Load
string activity1="active";
string activity2="Inactive";
sqlconnection con=new sqlconnection("give your connection string");
sqlcommand cmd=new sqlcommand();
cmd=new sqlcommand("select",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#activity",activity1);
String result1=cmd.ExecuteNonQuery();
sqlcommand cmd1=new sqlcommand();
cmd1=new sqlcommand("select",con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("#activity",activity2);
String result2=cmd.ExecuteNonQuery();
Label1.Text=result1;
Label2.Text=result2;
.aspx page
Welcome to my HomePage
Currently there are <asp:label id="Label1" runat="server"></asp:Label> Users active on my page and <asp:label id="Label2" runat="server"></asp:Label> that are inactive.
If you find this as useful and it solved your issue mark it as answer and give upvote.
Try this
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(#"MY CONNECTION");
SqlCommand com = new SqlCommand("Select Count(UserID) FROM users where Status = 'active'", con);
SqlDataReader dr;
con.Open();
while (dr.Read())
{
Debug.WriteLine(dr[0].ToString());
}
con.Close();
}
Related
I am using radsearchbox in my application. The data source is working and showing the values in dropdown but when I select an item in the dropdown the textbox is not populating. My code is
<telerik:RadSearchBox ID="RadSearchBox1" runat="server"
Filter="StartsWith" EnableAutoComplete="true" DataTextField="Name">
</telerik:RadSearchBox>
and in code
string comtext = "select Name from tblcustomer";
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
SqlCommand cmd = new SqlCommand(comtext, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
RadSearchBox1.DataSource = ds;
RadSearchBox1.DataBind();
Your issue might be that you are not using a SearchContext. But I think that you just need to specify the key field for the search box. Note below, the parameter DataValueField pointed at the ID for your table. (Change to your particular table id field)
<telerik:RadSearchBox ID="RadSearchBox1"
runat="server"
DataTextField="Name"
DataValueField="TableID" >
</telerik:RadSearchBox>
You would also need to change your query string to include the ID in the dataset.
string comtext = "select Name, TableID from tblcustomer";
Telerik's RadSearch demo page can give examples of how to use SearchContext if needed.
I would like to know how to load data from a database into a asp.net form.
So I use code to get the data via a query "Select * From ... Where ... = ..."
Then I load it into a reader and
While (reader.read)
{ string sProductName = Convert.ToString(reader[1]);
/*
Now I need to display this name and all the other data
(selling price, etc) on the form
But as I do not know how many products there will be it (the form) has to change
as the database information does (more products get added or removed).
*/
}
I do not know how to do that last part. How to make the data found display on screen.
Thanks !
The data I need to display is a the Product Name, the Product Description and the Product Selling Price underneath headings with those names thats all.
For SQL Database,
Take gridview control with id="gridview1" (whatever you like but use same id in code)
SqlConnection sql= new SqlConnection("your data base connection");
SqlCommand cmd = new SqlCommand("select * from your_table_name", sql);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gridview1.DataSource = ds;
gridview1.DataBind();
SqlConnection sql= new SqlConnection("your data base connection");
SqlCommand cmd = new SqlCommand("select * from your_table_name", sql);
sql.open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Textbox1.Text=dr[0].ToString();
Textbox2.Text=dr[1].ToString();
Textbox3.Text=dr[2].ToString();
......................;
}
con.close();
NOTE:
While working with datareader sql connection must be open.
I use textboxes to show the values.You can use where ever it is necessary .dr[0].ToString(),.dr[1].ToString() .....Contains the values of database table
My scenario is as follows.
I'm using ASP.NET 2.0 and VB.NET.
I have a login page. If I am an existing user, when I log in, my user details are popped in the respective textBoxes. Now I wish to change one of these fields. I edited one and then clicked on the Edit button to update the record. It's not updated but instead it brings the existing value from the text box.
How can I achieve this?
DBCmd.CommandText = "update IOMFNewMember set FirstName=#FirstName,MiddleName=#MiddleName,LastName=#LastNa,Gender=#Gender,DateofBirth=#DateofBirth,MartialStatus=#MartialStatus,DateofWedding=#DateofWedding,Nationality=#Nationality,ResidenceCountry=#ResidenceCountry,DateofJoiningIOMf=#DateofJoiningIOMf,EmailId=#EmailId,mobileno=#mobileno,AtPresent=#AtPresent,familykuwait=#familykuwait,DateofDeath =#DateofDeath where UserName=#Entery and MemberID=#MemberID"
For your requirement you have to run a sql command same as that u are using to populate the textboxes with the user details.
That is supposing you are using this in the login button click event
string strSql="select * from customers where id=" + txtLoginId.Text;
SqlConnection con=new SqlConnection(strCon);
con.Open();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=new SqlCommand(strSql,con);
DataSet dset=new DataSet();
da.Fill(dset);
con.Close();
You will have to use this in the edit button click event :
string strSql="update EmployeeTable set password = '" + txtPassword.Text + "'";
SqlConnection con=new SqlConnection(strCon);
con.Open();
SqlCommand cmd=new SqlCommand(strsql, con);
cmd.ExecuteScalar();
con.Close();
Hope this will clear you confusion.
This is just an example. For a real time situation its advisable to use stored procedures only for any database query.
I am using a datalist on customer page to list all customers. There is a page named "view" where I want to show the details of the particular selected customer. Here is the image button code:
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# GetImageURL(Eval("image")) %>' PostBackUrl='<%# Eval("id", "view.aspx?id={0}") %>'/></tr>
id is the unique field of the customer table. Now how should I retrieve the customer data on view.aspx, using the particular ID passed from the customer page. I use this code but it's not working:
Dim mycommand As New SqlCommand("SELECT * FROM customer where id = #id", con)
Int(ID = Request.QueryString("id"))
con.Open()
ProfileData.DataSource = mycommand.ExecuteReader
ProfileData.DataBind()
con.Close()
I just want to do that. In the customer page all customers are displayed with their image and when I click on any one of them then the details of the particular customer should be displayed on view.aspx page ..
you are using #id as CommandParameter to add this id as value to command parameter..
as
Dim mycommand As New SqlCommand("SELECT * FROM customer where id = #id", con)
mycommand.Parameters.Add("#id", SqlDbType.Int)
mycommand.Parameters("#id").Value = Convert.ToInt32( Request.QueryString("id"))
con.Open()
ProfileData.DataSource = mycommand.ExecuteReader
ProfileData.DataBind()
con.Close()
check these link to know about the parameter..
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
http://msdn.microsoft.com/en-us/library/yy6y35y8%28v=vs.80%29.aspx
I don't know how to go about this. I have a web form with dropdown list, named Recruiter. I have two SQL tables named Perm_Commision_Lookup & UserList.
One of the tables contain email and the other does not. The Perm_Commision_Lookup table does not contain recruiter's email, so I decided to Inner Join it with UserList table which contain recruiter emails. The LookupValue (Recruiter's display name) column from the Perm_Commision_Lookup table is what's displayed to the end user and the pk_LookupID column is the one that's inserted into the database.
What I want to achieve is this: When a user select let's say "John Doe" from the Recruiter dropdown list, I want to send out an email to John Doe alerting him that a form has been submitted and at the same time insert the selected value (pk_LookupID) into the database.
I see that the dropdown list has two field named: DataTextField="LookupValue" and DataValueField="pk_LookupID but how to get the User_Email is my major problem. Below is my SQL select...So far I can do a SQL INNER JOIN...which shown below and also I can display multiple columns into dropdown list...I'm also trying SQLDataReader but I'm just stop with how to get it done.
SELECT Perm_Commision_Lookup.pk_LookupID, Perm_Commision_Lookup.LookupValue, UserList.User_Email
FROM Perm_Commision_Lookup
INNER JOIN UserList ON Perm_Commision_Lookup.LookupUserName = UserList.GM_Username
Any help will be appreciated...
Thanks for your reply! Correct! It's linked via LookupUserName. That's what I have as for now...but still have the problem
Public Sub BindDropDownListData()
' connection string
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Using mySqlConnection As New SqlConnection(connectionString)
Try
' open the Sql connection
mySqlConnection.Open()
' Sql Command query to retrieve pk_LookupID, LookupValue, GM_Username, User_Email
Dim mySqlCommand As New SqlCommand(" SELECT Perm_Commision_Lookup.pk_LookupID, Perm_Commision_Lookup.LookupValue, UserList.GM_Username, UserList.User_Email FROM Perm_Commision_Lookup INNER JOIN UserList ON Perm_Commision_Lookup.LookupUserName = UserList.GM_Username order by LookupValue asc", mySqlConnection)
Dim mySqlDataAdapter As New SqlDataAdapter()
mySqlDataAdapter.SelectCommand = mySqlCommand
Dim myDataSet As New DataSet
' mySqlConnection.Open()
' Sql Data Reader to fetch the records row by row in forward direction.
Dim myDataReader As SqlDataReader = mySqlCommand.ExecuteReader()
'Perm_Commision_Lookup
If myDataReader.HasRows Then
' read each row fetched using DataReader
While myDataReader.Read()
Dim li As New ListItem()
'email = myDataReader("User_Email")
li.Value = myDataReader("pk_LookupID")
li.Text = myDataReader("LookupValue")
DropDownList1.Items.Add(li)
End While
End If
myDataReader.Close()
Catch ex As Exception
Label1.Text = ex.Message
Finally
' close the Sql Connection
mySqlConnection.Close()
End Try
DropDownList1.Items.Insert(0, New ListItem("Please Select Recruiter", ""))
End Using
End Sub
1: Use dropdown selectedIndexChanged event.
2: Inside the handler query the database to get the Email and send email.
3: You can selected lookupname from dropdown in the SelectedIndexChanged handler like myddl.SelectedItem.Text or you can get lookupid like myddl.SelectedValue.
From your query it seems your tables are linked via LookupUserName correct or is it LookupId?
And this might help.