Compilation error comes in query string in asp.net - asp.net

SqlConnection cn = new SqlConnection("server=localhost;initial catalog=newmits;trusted_connection=true");
cn.Open();
string q = string.Format("SELECT * from upnotice WHERE notice_type LIKE '{0}%'",t);
SqlDataAdapter adp = new SqlDataAdapter(q,cn);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
Error comes in these line pls help me

Try this:
string q = "SELECT * from upnotice WHERE notice_type like '%Tender%' OR notice_type like '%RecruitementNotice%'";

Related

How to store a select query result ( one result ) to a variable using executescalar() ? ( ASP.NET )

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();
}

Retrieving multiple rows from stored procedures

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;
}

need passing two parameters getting with one parameter value

This is my code. I am not able to get my desired output. I should pass parameters and retrieve either with CustomerCode or with CustomerName. Please help me. Thanks in advance and below I placed my sp
protected void txtsearch_Click2(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(myStr);
SqlCommand cmd = new SqlCommand("spRedemItem", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#CustomerCode", SqlDbType.Int).Value =
(DropDownList2.SelectedItem.Text== "int" ? Convert.ToInt32(txtkey2.Text) :(object)DBNull.Value);
cmd.Parameters.Add("#CustomerName", SqlDbType.NVarChar, 50).Value =
(DropDownList2.SelectedItem.Text == "string" ? txtkey2.Text : (object)DBNull.Value);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
Session["CustomerName"] = dt;
con.Open();
DataSet ds = new DataSet();
sda.Fill(ds);
dt = ds.Tables[0];
Label10.Text = dt.Rows[0]["ItemCode"].ToString();
Label11.Text = dt.Rows[0]["CustomerName"].ToString();
Label12.Text = dt.Rows[0]["PointsNeeded"].ToString();
GridView1.DataBind();
con.Close();
}
--my sp
alter Proc spRedemItem
(
#CustomerCode int=null,
#CustomerName nvarchar(50)=null
)
as
begin
select b.ItemCode,a.CustomerName,c.PointsNeeded from CustomerProfMain a
left join tb_Product b on a.CustomerCode=b.ItemCode
left join tb_RedemptionProducts c on b.Product_ID=c.ID
where (#CustomerCode is null or CustomerCode=#CustomerCode) or (#CustomerName is null or CustomerName=#CustomerName)
end
Instead of using,
where (#CustomerCode is null or CustomerCode=#CustomerCode)
OR (#CustomerName is null or CustomerName=#CustomerName)
you can try changing your WHERE clause like,
where (#CustomerCode is null or CustomerCode=#CustomerCode)
AND
(#CustomerName is null or CustomerName=#CustomerName)

How do I pass a querystring value into a select statement in ASP.NET?

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);

SqlDataAdapter with 2 queries doesnt give the right o/p

SqlDataAdapter adap = new SqlDataAdapter("Select * from Contentpagenav;Select * from Employees",conn);
DataSet ds = new DataSet();
conn.Open();
adap.TableMappings.Add("ContentPagenav", "Table1");
adap.TableMappings.Add("Employees", "Table2");
adap.Fill(ds, "Table1");
adap.Fill(ds, "Table2");
GridView1.DataSource = ds.Tables["Table1"];
GridView1.DataBind();
GridView2.DataSource = ds.Tables["Table2"];
GridView2.DataBind();
conn.Close();
I am getting the first table data in both the gridviews. What am I doing wrong?
did you tried by doing this
SqlDataAdapter adap = new SqlDataAdapter("Select * from Contentpagenav;Select * from Employees",conn);
DataSet ds = new DataSet();
conn.Open();
// don't map
//adap.TableMappings.Add("ContentPagenav", "Table1");
//adap.TableMappings.Add("Employees", "Table2");
//adap.Fill(ds, "Table1");
//adap.Fill(ds, "Table2");
// access tables by index instead of name
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView2.DataSource = ds.Tables[1];
GridView2.DataBind();
conn.Close();
Edit 1: There are some mistakes with your code
SqlDataAdapter adap = new SqlDataAdapter("Select * from Contentpagenav;Select * from Employees",conn);
DataSet ds = new DataSet();
conn.Open();
// the select statement only return Table, Table1, Table2 and so on
// it don't know About ContentPagenav or Employees
// so I am maping Table to MyTable1
//and Table1 to MyTable2
adap.TableMappings.Add("Table", "MyTable1");
adap.TableMappings.Add("Table1", "MyTable2");
// don't do this, it will discard your mapping,
// it will return
// Table1, Table2, and so on
//adap.Fill(ds, "Table1");
// just fill your dataset, and it will keep your mapping
adap.Fill(ds);
// access tables by your suggested name
GridView1.DataSource = ds.Tables["MyTable1"];
GridView1.DataBind();
GridView2.DataSource = ds.Tables["MyTable2"];
GridView2.DataBind();
conn.Close();
For more understandings Table Mapping in ADO.NET

Resources