I am currently doing a bank project which I could select the months of transaction history I want to view ( Eg: Current Month, Current Month n last 1 month ) Now, I only manage to retrieve all the transaction history I had using grid view. May I know how to view transactions for different period based on the radio button selected? I'm currently using asp.net n C#
myConnection.ConnectionString = strConnectionString;
SqlCommand cmd = new SqlCommand("SELECT thDate, thType, thAmountIn, thAmountOut from [Transaction]", myConnection);
myConnection.Open();
SqlDataReader reader1 = cmd.ExecuteReader();
GridView1.DataSource = reader1;
GridView1.DataBind();
For your SQL command you could try:
SqlCommand cmd = new SqlCommand("SELECT thDate, thType, thAmountIn, thAmountOut from [Transaction] where thDate=" + RadioButtonList1.Text + ", myConnection);
Where RadioButtonList1 is replaced by the name of your RadioButtonList. This should place the value of the selected radio button into the SQL statement.
Related
I'm creating a reservation system and this is my forms, I'm having a hard time trying to send the quantity of the products because I'm using multiple textboxes unlike the reservation id I can easily get the value because I'm only using one textbox
SqlConnection con = new SqlConnection(#"Data Source = MAAAYYK; Initial Catalog = dbFoodReservation; Integrated Security = True");
SqlCommand cm = new SqlCommand();
con.Open();
cm = new SqlCommand("Insert into tbl_Reservation(ReservationID,ReceiptID,Price,Qty)Values(#ReservationID, #ReceiptID, #Price, #Qty)", con);
cm.Parameters.AddWithValue("#ReservationID", txtBoxreserveID.Text);
cm.Parameters.AddWithValue("#ReceiptID", txtboxReceiptID.Text);
cm.Parameters.AddWithValue("#Price", txtboxTotal.Text);
cm.Parameters.AddWithValue("#Qty", txtfried.Text + txtbbq.Text);
cm.ExecuteNonQuery();
con.Close();
the blank one is there I don't know what to put because as you can see I'm in my form there are many textbox for the quantity
Is there a way for me to get those values like if loop or something?
I changed my code and found a way to send the value of qty but when I enter 1 and 1 it shows 11 in the database instead of 2
Here is the database
You must convert text to Integer for Prevent concatenate strings.
// clean code
var intTxtfried = Convert.ToInt32(txtfried.Text);
var intTxtbbq = Convert.ToInt32(txtbbq.Text);
var totalQty = intTxtfried + intTxtbbq;
// other code ...
cm.Parameters.AddWithValue("#Qty", totalQty);
Hi there guys i am sure this is quite easy i am just completely new to C#.
So I can read the SQL database and bring back results but I cant figure out how to read the returning result which should then be inserted into the Database.
Any idea how I can read the Result View because i can see the value i want in there. Value for this example is 'AS'
SqlConnection con = new SqlConnection("Data Source=###;Initial Catalog=######;Persist Security Info=True;User ID=##;Password=#######");
SqlCommand cmd = new SqlCommand("Select ISOCode from Countries Where CountryName like '" + CTRYLST.SelectedItem + "%'", con);
con.Open();
CTRYLST.Items.Clear();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
CTRYLST.Items.Add(sdr.GetString(0));
}
}
SqlCommand cmd2 = new SqlCommand("INSERT INTO CountryNoMail (ISOCode) VALUES ('" + CTRYLST.Items.ToString() + "');", con);
cmd2.ExecuteNonQuery();
con.Close();
Assuming the sql is correct and everything is fine with the reading, eventually you can inspect the values at runtime by adding a break in the line of additems
You will be able top get back your values by using
CTRYLST.Items[0].ToString;
But it is always preferrable to use a for and cycle since you cannot be sure how many items there will be in the list box. Be careful that items are zerobased, so the first element has 0 as index, the first has 1 and so on.
I am trying to update my database(Table) and make the active column = 1.
I have duplicate reports (subject_text) with different countries and parameters.
subject_text countries parameter1 active
usage GB 1 0
usage FR 2 0
usage PT 1 0
closed GB,FR,PT 1 0
Here is an example of what my database looks like(simplified, there are many more parameters and many more reports but i hope you can see what I mean by duplicate report names)
Here is my .cs file to show the update i am trying to carry out.The Subject_text are in drop-down list form so the user can select which report to update. These reports(subject_text) are hard-coded into my ASPX page. When selecting a report such as 'closed' the update works, but when it comes to updating reports which have different countries or parameters that where i'm having trouble.
Masterpage masteris linking this page to where the .getDropDownListValue is
How can I add to my update statement when the drop-down lists contain different elements?
protected void RunReport_Click (object sender, System.EventArgs e)
{
MasterPage master = (MasterPage)this.Master;
string sqlStatement = "";
sqlStatement = #"UPDATE [TODD].[dbo].[Table] SET Active='1' WHERE subject_text = #report";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sqlStatement, conn);
cmd = new SqlCommand(sqlStatement, conn);
cmd.Parameters.AddWithValue("#report", ddl_Report.SelectedItem.Text);
string getcountry = master.getDropDownListValue(ddl_country, false);
if (!string.IsNullOrWhiteSpace(getcountry))
{
cmd.Parameters.AddWithValue("#country", getcountry);
sqlStatement += "AND countries = #country";
}
string getparam1 = master.getDropDownListValue(Param1, false);
if (!string.IsNullOrWhiteSpace(getparam1))
{
cmd.Parameters.AddWithValue("#param1", getparam1);
sqlStatement += "AND parameter1 = #param1";
}
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Thank you for your time.
move
SqlCommand cmd = new SqlCommand(sqlStatement, conn);
to below the final calculation of your string sqlStatement
adding new text to sqlStatement should be done before you create your SqlCommand based on sqlStatement
also, you do not need to call new SqlCommand(sqlStatement, conn); twice
Why don't you just pass the identity? This is much cleaner.
UPDATE [TODD].[dbo].[Table] SET Active='1' WHERE RecordID = #RecordId
I have three dropdown list one for name, other month and other is year values. when I select name in dropdown list grid view display values of selected name. when I select month it has to display according to the month. when I select both name and month it has to display according to that. when I select all the three grid view has to shows the values for that condition.
Now am binding the data for dropdown list and grid view is in code behind file.
How to achieve that?
Guide me for ASPX and C# coding.
You have to create a dynamic function which accepts the parameters as you select the choices from the drop down.
public void BindGridView(string Name, string Month, string year)
{
//logic to fetch the data
}
Then you have to check the parameters are null or not. Based on these results you can put the queries to fetch the data.
I achieve that by using following coding.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sivConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT EMPNAME,CONVERT(VARCHAR(11),SALDATE,113),BAS_SAL,BASPAY,ADJ,DA,HRA,MA,TOT_SAL,EPF,FA,CATUP,NET_SAL from salentry where empname ='" + DropDownList1.SelectedValue + "' or DATEPART(MONTH,saldate)='" + DropDownList2.SelectedValue + "' or DATEPART(YEAR,saldate)='"+DropDownList3.SelectedValue+"'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();`
Do you have idea better than that?
I have database table leave_rec(name,date1,leave,ltype), a Dropdown list and a gridview.
I want to do such that,when I select month(e.g. february) in dropdown list the gridview should display all table values for february only(e.g.rohan leuva,2/28/2013,full,casual),means record which has month=2 (february).
How to overcome this issue? I tried but I can only display all the values in gridview at this moment. Any help would be greatly appriciated.
SqlConnection conn = new SqlConnection();
conn.ConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand("select date1,leave,ltype from leave_rec where name='" + DropDownList1.SelectedValue + "'", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
The above code displays the date1,leave,ltype for dropdownlist1.selectedvalue. But now i want to have second dropdown in which months will be there. so when i select february in second one, grid should display value for dropdownlist1.selectedvalue for february only.
First, your query needs to be something like this:
select date1, leave, ltype from leave_rec where MONTH(date1) = 2 // February
Then, integrating it into your code:
SqlCommand cmd = new SqlCommand("select date1, leave, ltype from leave_rec where MONTH(date1) = #p1", conn);
cmd.Parameters.Add(new SqlParameter("p1", combo.SelectedKey));
Use parameters instead of string concatenation to avoid SQL Injection, see an example here: http://www.dotnetperls.com/sqlparameter
(Use your own control names for "combo.SelectedKey", of course)
I think problem in query , instead of name you have to write date1
SqlCommand cmd = new SqlCommand("select date1, leave, ltype from leave_rec where MONTH(date1) ='" + DropDownList1.SelectedValue + "'", conn);
Convert the dataset to DataTable
then - Filter it by dt.Filter=monthName.toString()
then bind it to GridView - dt.DefaultView;
Agree with #Saurabh, look into the use of Linq and Stored Procedures to force the use of types and modelling.