Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8260539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:07:22+00:00 2026-06-08T03:07:22+00:00

This is a question from an experienced beginner! Using ASP.NET 4 C# AND SQL

  • 0

This is a question from an experienced beginner!

Using ASP.NET 4 C# AND SQL server,

I have a connection string in web.config to myDatabase named “myCS”.
I have a database named myDB.
I have a table named myTable with a primary key named myPK

What are the NECESSARY lines of code behind (minimal code) to create a SQL connection, then select from myTable where myPK==”simpleText”

it will probably include:

sqlconnection conn = new sqlconnection(??? myCS)
string SQLcommand = select * from myDB.myTable where myPK==myTestString;
sqlCommand command = new SqlCommand(SQL,conn);

conn.Open();

booleanFlag = ????

conn.Close();
conn.Dispose();

then

If ( theAnswer  != NULL )  // or (if flag)
{
Response.Redirect("Page1.aspx");
}
else
{
Response.Redirect("Page2.aspx");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:07:24+00:00Added an answer on June 8, 2026 at 3:07 am

    Here is a limited simple tutorial:

    First, you want to have a class to do the hard work for you, then you will use it with ease.

    First, you have to crate the connection string in your web.config file and name it.
    Here it is named DatabaseConnectionString, but you may named it myCS as required in the question.

    Now, in App_Code create a new class file and name it SqlComm (this is just an example name) like:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    
    public class SqlComm
    {
        // this is a shortcut for your connection string
        static string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbConStr"].ConnectionString;
    
        // this is for just executing sql command with no value to return
        public static void SqlExecute(string sql)
        {
            using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }
        }
    
    // with this you will be able to return a value
        public static object SqlReturn(string sql)
        {
            using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                object result = (object)cmd.ExecuteScalar();
                return result;
            }
        }
    
        // with this you can retrieve an entire table or part of it
        public static DataTable SqlDataTable(string sql)
        {
            using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Connection.Open();
                DataTable TempTable = new DataTable();
                TempTable.Load(cmd.ExecuteReader());
                return TempTable;
            }
        }   
    
    // sooner or later you will probably use stored procedures. 
    // you can use this in order to execute a stored procedure with 1 parameter
    // it will work for returning a value or just executing with no returns
        public static object SqlStoredProcedure1Param(string StoredProcedure, string PrmName1, object Param1)
        {
            using (SqlConnection conn = new SqlConnection(DatabaseConnectionString))
            {
                SqlCommand cmd = new SqlCommand(StoredProcedure, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter(PrmName1, Param1.ToString()));
                cmd.Connection.Open();
                object obj = new object();
                obj = cmd.ExecuteScalar();
                return obj;
            }
        }
    }
    

    Okay, this only a class, and now you should know how to use it:

    If you wish to execute a command like delete, insert, update etc. use this:

    SqlComm.SqlExecute("TRUNCATE TABLE Table1");
    

    but if you need to retrieve a specific value from the database use this:

    int myRequiredScalar = 0;
    object obj = new object();
    obj = SqlComm.SqlReturn("SELECT TOP 1 Col1 FROM Table1");
    if (obj != null) myRequiredScalar = (int)obj;
    

    You can retrieve a bunch of rows from the database this way (others like other ways)
    This is relevant to your sepecific question

    int Col1Value = 0;
    DataTable dt = new DataTable();
    dt = SqlComm.SqlDataTable("SELECT * FROM myTable WHERE myPK='simpleText'");
    if (dt.Rows.Count == 0) 
    {
        // do something if the query return no rows
        // you may insert the relevant redirection you asked for
    }
    else
    {
        // Get the value of Col1 in the 3rd row (0 is the first row)
        Col1Value = (int)dt.Rows[2]["Col1"];
    
        // or just make the other redirection from your question
    }   
    

    If you need to execute a stored procedure with or without returning a value back this is the way to do that (in this example there are no returning value)

    SqlComm.SqlStoredProcedure1Param("TheStoredProcedureName", "TheParameterName", TheParameterValue);
    

    Again, for your specific question return the table using the SqlDataTable , and redirect if dt.Rows.Count >0

    Have fun.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question originates from a discussion on whether to use SQL ranking functionality or
I'm pretty experienced in C#, but still mostly a beginner in SQL. We have
This question is not coming from a programmer. (obviously) I currently have a programmer
I have an ASP.NET WebForms control (derived from Control, not WebControl, if it helps)
This is a continuation of this question from yesterday . Here are my three
I got this question from my cousin: What will be the best way to
Unfortunately I am not writing this question from my Developing PC so I might
This question results from hours of googling highstocks, zoom, extremes, ranges, and every other
This question stems from Hartl's Rails Tutorial (progressed in chapter 9) - sorry if
This question arose from the discussion in the comments of this answer . First,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.