need passing two parameters getting with one parameter value - asp.net

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)

Related

how to check in if condition it is string or not

in my search function i need to pass two parameters to SP.Here i kept if condition for that.But am not getting required output. here is my code.any one help me
if (IsValid)
{
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(myStr);
SqlCommand cmd = new SqlCommand("spRedemItem", con);
cmd.CommandType = CommandType.StoredProcedure;
if(Parameter.Equals(DropDownList2.SelectedValue=="CustomerCode"))
{
cmd.Parameters.AddWithValue("#CustomerCode", txtkey2.Text);
}
else
{
cmd.Parameters.AddWithValue("#CustomerName", txtkey2.Text);
}
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();
// Session["CustomerName"] = dt;
View.DataBind();
con.Close();
}
If your sproc has two parameters then you need to pass two parameters every time. Generally you would write your SQL code such that you can just pass NULL to any parameters that you want to ignore, e.g. WHERE (#Column1 IS NULL OR Column1 = #Column1). You then use DBNull.Value for the parameter value if you want to ignore that parameter. You can't use AddWithValue though, because a data type can't be inferred.
E.g.
command.CommandText = #"SELECT *
FROM MyTable
WHERE (#C1 IS NULL OR C1 = #C1)
AND (#C2 IS NULL OR C2 = #C2)";
command.Parameters.Add("#C1", SqlDbType.Int).Value = (someValue == "int"
? Convert.ToInt32(myTextBox.Text)
: (object) DBNull.Value);
command.Parameters.Add("#C2", SqlDbType.VarChar, 50).Value = (someValue == "string"
? myTextBox.Text
: (object) DBNull.Value);

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

Compilation error comes in query string in 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%'";

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