dconnetion = New OdbcConnection(lsconnectionstring)
dconnetion.Open()
mssql = "select cust_id,cust_name,cust_address,cust_contact_no from cust_details where cust_id in (select cust_id from cust_details)"
cmd.Connection = dconnetion
cmd.CommandText = mssql
cmd.CommandType = CommandType.Text
dr = cmd.ExecuteReader
I use this statement to read data from a database using DataReader.
The problem is that when I use DataSet instead of DataReader what will the query is?
Using DataAdapter.Fill() method with the same query.
dconnetion = new OdbcConnection(lsconnectionstring);
//dconnetion.Open()
mssql = "select cust_id,cust_name,cust_address,cust_contact_no from cust_details where cust_id in (select cust_id from cust_details)";
cmd.Connection = dconnetion;
cmd.CommandText = mssql;
cmd.CommandType = CommandType.Text;
using (OdbcDataAdapter da = new OdbcDataAdapter(cmd))
{
DataSet ds = new DataSet();
dconnetion.Open();
da.Fill(ds);
dconnetion.Close();
}
Related
i have to store a select query result in a variable .i'm new in asp.net . i used executescalar but it doesn't work. i try many times but i failed here my last try :
using (SqlConnection sqlConnection = new SqlConnection())
{
var connetionString = ConfigurationManager.ConnectionStrings["connections"].ToString();
sqlConnection.ConnectionString = connetionString;
string sql = "Select sum((prime_comptant+10)*0.12) from mvt_garantie_quittance where numero_quittance='" + numQuittance + "'";
SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlConnection);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
string result = dataset.Tables[0].ToString();
}
Can you fix the code to me? i have to store the result in a variable
string sql = "Select sum((prime_comptant+10)*0.12) from mvt_garantie_quittance where numero_quittance='" + numQuittance + "'";
var connetionString = ConfigurationManager.ConnectionStrings["connections"].ToString();
string result = null;
using (SqlConnection conn = new SqlConnection(connetionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
result = cmd.ExecuteScalar().ToString();
}
My stored procedure proc_search returns only the name on execution and I have been using the following code in ASP.NET to display the value...
SqlCommand cmd = new SqlCommand("proc_search", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#branch", SqlDbType.VarChar).Value = branchidtext.Text;
cmd.Parameters.Add("#Acct", SqlDbType.VarChar).Value = accountidtext.Text;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
nametext.Text = reader[0].ToString();
}
If I have a procedure which returns multiple columns and multiple rows like Name, Address, Age... How do I display it in the text boxes? Please help.
If you know exact order of data you can say
while (reader.Read())
{
nametext.Text = reader[0].ToString();
agetext.Text = reader[1].ToString();
addresstext.Text = reader[2].ToString();
}
etc... If you don't know the ordering than say
while (reader.Read())
{
nametext.Text = reader["Name"].ToString();
agetext.Text = reader["Age"].ToString();
addresstext.Text = reader["Address"].ToString();
}
Use this kind of method. This will retun dataset having multiple rows and cols
public DataSet GetDataSet()
{
SqlConnection conn = new SqlConnection(con);
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "proc_search";
cmd.CommandType = CommandType.StoredProcedure;
da.SelectCommand = cmd;
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
return ds;
}
i have two Queries and for that two Queries i had taken two Grid Views
select course_name , start_date, end_date, timings, fee, branch_code from coursesprovided where start_date>=Getdate() and branch_code='Ameerpet'
select course_name , start_date, end_date, timings, fee, branch_code from coursesprovided where start_date>=Getdate() and branch_code='Hi-Tech City'
During my page load i need to display the Data in Grid View.
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=(local); Initial Catalog=gateway; User Id=sa; Password=wilshire#rnd; Integrated Security=false";
//Assigning Query
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select course_name , start_date, end_date, timings, fee, branch_code from coursesprovided where start_date>=Getdate() and branch_code='Ameerpet'";
cmd.CommandText = "select course_name , start_date, end_date, timings, fee, branch_code from coursesprovided where start_date>=Getdate() and branch_code='Ameerpet'";
cmd.CommandType = CommandType.Text;
//Execute the COmmand
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
gvap.DataSource = ds;
gvap.DataBind();
}
is There any alternative rather than taking 2 SQLcommands, 2 SqlDataAdapter, 2 Dataset.....
Could you not just combine those two queries into one?
SELECT [course_name], [start_date], [end_date], [timings], [fee], [branch_code] FROM [coursesprovided] WHERE [start_date] > = GETDATE() AND ([branch_code] ='Ameerpet' OR [branch_code] = 'Hi-Tech City')
Keep in mind that you should avoid hard-coding field values like that if at all possible, and use parameters instead.
String branchCode1 = "Ameerpet";
String branchCode2 = "H-Tech City";
cmd.CommandText = "SELECT [course_name], [start_date], [end_date], [timings], [fee], [branch_code] FROM [coursesprovided] WHERE [start_date] > = GETDATE() AND ([branch_code] = #BranchCode1 OR [branch_code] = #BranchCode2)";
da.SelectCommand.Parameters.AddWithValue("BranchCode1", branchCode1);
da.SelectCommand.Parameters.AddWithValue("BranchCode2", branchCode2);
That block of code is simplified...you could pass in the parameter values in any way that you wanted, and you should probably keep things clean by defining the entire thing as a separate function or stored procedure, so you could pass in whatever value you wanted. I just defined the two parameters in the code for illustration purposes.
How do I pass a querystring value into a select statement in ASP.NET?
Here's what I've tried:
{
string myID = (Request.QueryString["ID"] ?? "0").ToString();
SqlConnection con = new SqlConnection
(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT ID, Name, Date, Description FROM MyTable
where ID=#ID", con);
DataTable dt = new DataTable();
da.Fill(dt);
GV_InlineEditing.DataSource = dt;
GV_InlineEditing.DataBind();
}
Use a SqlCommand and add the parameter to it:
string query = "SELECT ID, Name, Date, Description FROM MyTable where ID=#ID";
var cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("#ID", myID);
var da = new SqlDataAdapter(cmd);
2 questions for everybody.
1) How can I order the years by their value, it crashes when I use DESC?
2) If I populate my list like so:
string strConn = ConfigurationManager.ConnectionStrings["rde_410978ConnectionString"].ToString();
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select Distinct Year from MonthlySales DESC"; //DESC DOESNT WORK?
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
ddItems.DataSource = objDs.Tables[0];
ddItems.DataTextField = "Year";
ddItems.DataValueField = "Year";
ddItems.DataBind();
ddItems.Items.Insert(0, "Select");
}
How can I make the year selected appear under ddItems.SelectedItem?
WHERE Year = " + ddItems.SelectedItem + "GROUP BY Name ";
That part of another query doesn't work when I populate my list dynamically, any reasons why/ how can I fix it.
Regards.
EDIT:
To make my second question clearer, after debugging its always selecting the top item in the drop down list not the actual selected item?
First, in your sql you are missing "order by"...use this
"Select Distinct Year from MonthlySales order by Year DESC"
Second, you need to make use of the SelectedValue property to get your dropdown's selected value...as below...
WHERE Year = " + ddItems.SelectedValue + " GROUP BY Name";
Having said that, I strongly recommend you to use..."parameterized" sql...Here is an example on how you could enable parameterized sql query...
Give me parameterized SQL, or give me death
Update:
Looks like you are binding your dropdown on every post back...you may try this...
if (!Page.IsPostBack && objDs.Tables[0].Rows.Count > 0)
{
ddItems.DataSource = objDs.Tables[0];
ddItems.DataTextField = "Year";
ddItems.DataValueField = "Year";
ddItems.DataBind();
ddItems.Items.Insert(0, "Select");
}
Ans 1)
cmd.CommandText = "Select Distinct Year from MonthlySales ORDER BY 1 DESC"
You are missing order by. Here it is.
"Select Distinct Year from MonthlySales order by Year DESC";
For your second part you can do this. Please mind the space in " GROUP BY Name"
WHERE Year = " + ddItems.SelectedItem.Text + " GROUP BY Name ";
using (SqlConnection con = new SqlConnection(strConn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select Distinct Year from MonthlySales Order By DESC";
using (DataSet objDs = new DataSet())
{
using (SqlDataAdapter dAdapter = new SqlDataAdapter())
{
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
ddItems.DataSource = objDs.Tables[0];
ddItems.DataTextField = "Year";
ddItems.DataValueField = "Year";
ddItems.DataBind();
ddItems.Items.Insert(0, "Select");
}
}
}
}
}