locked
how execute scalar run without connection strings? RRS feed

  • Question

  • User-1026236167 posted

    how to use execute scalar without connection strings in my code

    when i remove connection strings then error occur execute scalar needs a seprate connection

    how to use execute scalar in data adapter adp

    please execute them

    public void FillGridview()
    {

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT MAX(tbl_id) + 1 as tbl_id FROM tbl_licencel", con);
    user_id.Text = cmd.ExecuteScalar().ToString();
    con.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    FillGridview();

    }
    }

    aspx

    here is my textbox

    <div style="margin-top:-90px; margin-left:700px">
    <b>User id</b>&nbsp &nbsp &nbsp<asp:TextBox ID="user_id" Enabled="false" runat="server" style="width:400px"></asp:TextBox>
    <br />

    </div>

    Wednesday, June 3, 2020 6:33 AM

Answers

  • User288213138 posted

    Hi prabhjot1313,

    sir my problem is how this code can be run in the data adapter

    The SqlCommand.ExecuteScalar method returns the first column of the first row in the result set returned by the query.

    And the SqlDataAdapter class represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database.

    So it is not recommended to use ExecuteScalar and SqlDataAdapter together.

    If you want to use SqlDataAdapter to bind data source to GridView, then you can try below code:

     string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                string query = "SELECT * FROM Customer";
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            GridView1.DataSource = dt;
                            GridView1.DataBind();
                        }
                    }
                }
    

    If you still have questions, please let me know.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2020 2:10 AM

All replies

  • User288213138 posted

    Hi prabhjot1313,

    how to use execute scalar without connection strings in my code

    when i remove connection strings then error occur execute scalar needs a seprate connection

    how to use execute scalar in data adapter adp

    According to your description, I couldn’t understand your requirement clearly.

    If there is no connection strings, then you can’t be sure which database you want to query.

    Or you mean you don’t want to put the details of the connection strings in the C# code, if it is, then you can put it in the web.config file.

    <connectionStrings>	
        <add name="constr" connectionString="" />
    </connectionStrings>

    please execute them

    public void FillGridview()
    {

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT MAX(tbl_id) + 1 as tbl_id FROM tbl_licencel", con);
    user_id.Text = cmd.ExecuteScalar().ToString();
    con.Close();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    FillGridview();

    }
    }

    This code can works fine with me.

    Best regards,

    Sam

    Wednesday, June 3, 2020 8:11 AM
  • User-1026236167 posted

    sir my problem is how this code can be run in the data adapter

    Wednesday, June 3, 2020 11:58 AM
  • User753101303 posted

    Hi,

    ExecuteScalar is to return a single value so it doesn't seems to make sense to use a gridview to show that. See https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldataadapter?view=dotnet-plat-ext-3.1 for how to use a SqlDataAdapter (which uses a SqlCommand which uses a SqlConnection which uses a  connectino string).

    See also maybe https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataadapters-and-datareaders to see if you really need a DataAdapter...

    Wednesday, June 3, 2020 12:14 PM
  • User288213138 posted

    Hi prabhjot1313,

    sir my problem is how this code can be run in the data adapter

    The SqlCommand.ExecuteScalar method returns the first column of the first row in the result set returned by the query.

    And the SqlDataAdapter class represents a set of data commands and a database connection that are used to fill the DataSet and update a SQL Server database.

    So it is not recommended to use ExecuteScalar and SqlDataAdapter together.

    If you want to use SqlDataAdapter to bind data source to GridView, then you can try below code:

     string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                string query = "SELECT * FROM Customer";
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter(query, con))
                    {
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            GridView1.DataSource = dt;
                            GridView1.DataBind();
                        }
                    }
                }
    

    If you still have questions, please let me know.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 4, 2020 2:10 AM
  • User-1026236167 posted

    sir i think without connection strings it is impossible to use execute scalar?

    Friday, June 26, 2020 10:29 AM
  • User753101303 posted

    The purpose of a connection string is to tell on which server and database, your SQL statement should run. So technically speaking it is always needed. It may not be apparent as if you don't have an explicit one your code can still use a default connection string taken from the machine.config file (and for example using a db name based on the DbContext name if using EF).

    So you may skip that on a development machine but more likely you'll always end up in configuring explicitely a connection string for a production app.

    If you have a particular problem it could be easier to discuss directly about it.

    Friday, June 26, 2020 11:26 AM