ASP.NET - combine results of two queries
Asked By Rahul
03-Feb-12 06:25 AM

how to bring this codings into one using datareader or if else or anything ?
1.
cmd=new SqlCommand("select Count(*) from agentreg where userid='" +TextBox1.Text+ "' and username='"+TextBox2.Text+"'",con);
int i = (int)cmd.ExecuteScalar();
if (i != 0)
{
cmd =
new SqlCommand("select count (username) from creditadd where services ='" + DropDownList1.SelectedItem.Value + "' and username='" + TextBox2.Text + "' and userid='" + TextBox1.Text + "'", con);
int j = (int)cmd.ExecuteScalar();
if (j == 0)
{
cmd = new SqlCommand("insert into creditadd values (@userid,@username,@services,@credit)", con);
cmd.Parameters.AddWithValue(
"userid", TextBox1.Text);
cmd.Parameters.AddWithValue("username", TextBox2.Text);
cmd.Parameters.AddWithValue(
"services", DropDownList1.SelectedItem.Value);
cmd.Parameters.AddWithValue("credit", TextBox3.Text);
cmd.ExecuteNonQuery();
TextBox1.Text =
"";
TextBox2.Text = "";
TextBox3.Text =
"";
Response.Write("<script>alert('Successfull');</script>");
bindgrid();
dropbind();
}
else
{
Response.Write("<script>alert('Already Exist');</script>");
}
}
else
{
Response.Write("<script>alert('Not Registered');</script>");
}
2.
cmd =
new SqlCommand("select Count(*) from memreg where userid='" + TextBox1.Text + "' and username='" + TextBox2.Text + "'", con);
int k = (int)cmd.ExecuteScalar();
if (k != 0)
{
cmd = new SqlCommand("select count (username) from creditadd where services ='" + DropDownList1.SelectedItem.Value + "' and username='" + TextBox2.Text + "' and userid='" + TextBox1.Text + "'", con);
int l = (int)cmd.ExecuteScalar();
if (l == 0)
{
cmd =
new SqlCommand("insert into creditadd values (@userid,@username,@services,@credit)", con);
cmd.Parameters.AddWithValue("userid", TextBox1.Text);
cmd.Parameters.AddWithValue(
"username", TextBox2.Text);
cmd.Parameters.AddWithValue("services", DropDownList1.SelectedItem.Value);
cmd.Parameters.AddWithValue(
"credit", TextBox3.Text);
cmd.ExecuteNonQuery();
TextBox1.Text = "";
TextBox2.Text =
"";
TextBox3.Text = "";
Response.Write(
"<script>alert('Successfull');</script>");
bindgrid();
dropbind();
}
else
{
Response.Write("<script>alert('Already Exist');</script>");
}
}
else
{
Response.Write("<script>alert('Not Registered');</script>");
}
D Company replied to Rahul

Text = = "All" && DropDownList_state.SelectedItem.Text = = "All" && DropDownList_city.SelectedItem.Text = = "All" && CheckBoxList1.SelectedIndex = = -1) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = true && CheckBoxList1.Items[1].Selected = = false && CheckBoxList1.Items[2].Selected = = false ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = false && CheckBoxList1.Items[1].Selected = = true && CheckBoxList1.Items[2].Selected = = false ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = false && CheckBoxList1.Items[1].Selected = = false && CheckBoxList1.Items[2].Selected = = true ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = true && CheckBoxList1.Items[1].Selected = = true && CheckBoxList1.Items[2].Selected = = false ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = true && CheckBoxList1.Items[1].Selected = = false && CheckBoxList1.Items[2].Selected = = true ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = false && CheckBoxList1.Items[1].Selected = = true && CheckBoxList1.Items[2].Selected = = true ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = true && CheckBoxList1.Items[1].Selected = = true && CheckBoxList1.Items[2].Selected = = true ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area
Text = = "All" && DropDownList_state.SelectedItem.Text = = "All" && DropDownList_city.SelectedItem.Text = = "All" && CheckBoxList1.SelectedIndex = = -1) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = true && CheckBoxList1.Items[1].Selected = = false && CheckBoxList1.Items[2].Selected = = false ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = false && CheckBoxList1.Items[1].Selected = = true && CheckBoxList1.Items[2].Selected = = false ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = false && CheckBoxList1.Items[1].Selected = = false && CheckBoxList1.Items[2].Selected = = true ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = true && CheckBoxList1.Items[1].Selected = = true && CheckBoxList1.Items[2].Selected = = false ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = true && CheckBoxList1.Items[1].Selected = = false && CheckBoxList1.Items[2].Selected = = true ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = false && CheckBoxList1.Items[1].Selected = = true && CheckBoxList1.Items[2].Selected = = true ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area 0].Selected = = true && CheckBoxList1.Items[1].Selected = = true && CheckBoxList1.Items[2].Selected = = true ) { cmd = new SqlCommand ( "select a.img, a.prop_name, b.state_name, c.city_name, d.price, d.prop_price_type, d.builtup_area
SqlHelper()" private SqlHelper() {} / / / <summary> / / / This method is used to attach array of SqlParameters to a SqlCommand. / / / / / / This method will assign a value of DbNull to any parameter with a direction of commandParameters"> An array of SqlParameters to be added to command< / param> private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters) { if( command = = null ) throw new ArgumentNullException( "command" ); if( commandParameters ! = null ) { foreach (SqlParameter connection, transaction, command type and parameters / / / to the provided command / / / < / summary> / / / <param name = "command"> The SqlCommand to be prepared< / param> / / / <param name = "connection"> A valid SqlConnection, on which to execute this the connection was opened by the method, otherwose is false.< / param> private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection ) { if null) { AttachParameters(command, commandParameters); } return; } #endregion private utility methods & constructors #region ExecuteNonQuery / / / <summary> / / / Execute a SqlCommand (that returns no resultset and takes no parameters) against the database specified in / / / the connection for the set of SqlParameters return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null); } / / / <summary> / / / Execute a SqlCommand (that returns no resultset) against the database specified in the connection string / / / using the provided connection string return ExecuteNonQuery(connection, commandType, commandText, commandParameters); } } / / / <summary> / / / Execute a stored procedure via a SqlCommand (that returns no resultset) against the database specified in / / / the connection string using the provided just call the SP without params return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName); } } / / / <summary> / / / Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlConnection. / / / < / summary> / / / <remarks> / / / e
Sqlcommand Hi All I am new to .Net. Some one tell me how to use SqlCommand to insert, update, delete, select with neat example. Thanks In Advance. A SqlCommand object allows you to specify what type of interaction you want to perform with a select, insert, modify, and delete commands on rows of data in a database table. The SqlCommand object can be used to support disconnected data management scenarios, but in this lesson we will only use the SqlCommand object alone. A later lesson on the SqlDataAdapter will explain how to implement an application value from a database, such as the number of records in a table. Creating a SqlCommand Object Similar to other C# objects, you instantiate a SqlCommand object via the new instance declaration, as follows: SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn); The line above is typical for instantiating
SqlConnection("server = (local);database = ;") Dim Obread As SqlDataReader Dim Obread1 As SqlDataReader Dim Obcomm As SqlCommand Dim Obcomm1 As SqlCommand Private Sub First_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase Load Dim i As Integer Dim temp As String Dim Obcomm As New SqlCommand ThisWeekKeyword = "yz" 'ThisWeekKeyword = "dp" Try Obcomm = New SqlCommand("select top 1 * from TT_List where type = 1 order by time_rcv desc", Obconn) Obconn.Open i) = temp Next Else MessageBox.Show("Error from First_Load") End If Obconn.close() Obcomm = New SqlCommand("select top 1 * from TT_List where type = 2 order by time_rcv desc", Obconn) Obconn.Open i As Integer Dim Temp_counter As Integer Dim The_real As String Temp_counter = 0 Obcomm = New SqlCommand("select top 1 * from TT_MO where read_flag = 0 order by id", Obconn) Obconn.Open() Obread enrol command ElseIf LCase(content) Like "tton*" Or LCase(content) Like "ontt*" Then Obcomm = New SqlCommand("select * from TT_member where mobile_no like '" & Source_Mobtel & "'", Obconn) Obconn.Open() Obread = Obcomm.ExecuteReader() 'MessageBox.Show set profile ElseIf LCase(content) Like "ttp" Or LCase(content) Like "ttm" Then Obcomm = New SqlCommand("select * from TT_member where mobile_no like '" & Source_Mobtel & "'", Obconn) Obconn.Open() Obread = Obcomm.ExecuteReader() 'MessageBox.Show