Avatar of stellyuk
stellyuk

asked on 

Insert a value into an sql statement

Hey all,
I have this...
        int PCID = Profile.PCID;

        DataSet CompSet = SQLQueries.CompDetailBindData(PCID);
        DataRow Comp = (int) CompSet.Tables[0].Rows[0].ItemArray[0];

I'm trying to insert a value into a sql statement...

Am I going about it the right way??

Stelly
C#

Avatar of undefined
Last Comment
stellyuk
Avatar of gelbert
gelbert

Your code is attempting to set DataRow object to "int" value (last line). Did you mean something like this:
 DataRow Comp = CompSet.Tables[0].Rows[0];
Could you be more specific into which sql statement  do you want to insert your value(int) ? How this sql statement is stored: as string ?
Avatar of stellyuk
stellyuk

ASKER

here is the sql statement

    public static DataSet CompDetailBindData(int PCID)
    {
        string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
        System.Data.SqlClient.SqlConnection sqlconnection = new System.Data.SqlClient.SqlConnection(connectionString);

        string queryString = "SELECT * FROM computer WHERE PC_ID = @PCID";
        System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand(queryString, sqlconnection);

        System.Data.SqlClient.SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter(sqlcommand);
        sqlcommand.Parameters.Add("@PCID", System.Data.SqlDbType.Int).Value = PCID;
       
        DataSet ds = new DataSet();
        ad.Fill(ds);
        return ds;
    }


Stelly
Avatar of stellyuk
stellyuk

ASKER

more info...
I want to be able to do this...
    protected void Page_Load(object sender, EventArgs e)
    {
        int PCID = Profile.PCID;

        DataSet CompSet = SQLQueries.CompDetailBindData(PCID);
        DataRow Comp = CompSet.Tables[0].Rows[0];

        LabelCompModel.Text = Comp("PcModel");
        TextBoxCCCSR.Text = Comp("CCSR");
        TextBoxEPSR.Text = Comp("EPSR");
        TextBoxShipDate.Text = Comp("ShipDate");

    }
}
Avatar of gelbert
gelbert

Change round brackets to square ones and add ".ToString()" in

LabelCompModel.Text = Comp("PcModel");
        TextBoxCCCSR.Text = Comp("CCSR");
        TextBoxEPSR.Text = Comp("EPSR");
        TextBoxShipDate.Text = Comp("ShipDate");
to
LabelCompModel.Text = Comp["PcModel"].ToString();
        TextBoxCCCSR.Text = Comp["CCSR"].ToString();
        TextBoxEPSR.Text = Comp["EPSR"].ToString();
        TextBoxShipDate.Text = Comp["ShipDate"].ToString();
Avatar of stellyuk
stellyuk

ASKER

Getting a problem with the following line now its saying... "There is no row at position 0"
DataRow Comp = CompSet.Tables[0].Rows[0];

Stelly

Avatar of gelbert
gelbert

it means that your table CompSet.Tables contains 0 rows. Add:
 if ( CompSet.Tables.Rows.Count > 0 )
{
   .. do something
}
Avatar of stellyuk
stellyuk

ASKER

where??

Stelly
Avatar of stellyuk
stellyuk

ASKER

I get  System.Data.DataTableCollection does not contain a definition for Rows

Stelly
ASKER CERTIFIED SOLUTION
Avatar of gelbert
gelbert

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of stellyuk
stellyuk

ASKER

Ok its now popping up but without any data in the fields... could it be this code??

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CompList.DataSource = SQLQueries.CompListBindData();
        CompList.DataBind();
    }

    protected void CompList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button info = e.Row.FindControl("Info") as Button;
            info.Attributes.Add("onclick", "window.showModalDialog('CompDetails.aspx');");

        }
    }


    public void CompList_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "info")
        {
           
            int PCNo = Convert.ToInt32(e.CommandArgument);

            PCNo = Profile.PCID;
        }
    }

thanks,

Stelly
Avatar of stellyuk
stellyuk

ASKER

anyone?

Stelly
Avatar of gelbert
gelbert

Are you sure that your SQL query returns data ?
Avatar of stellyuk
stellyuk

ASKER

Yes... I have tried it using a standard valus of 1 and it returns a value...

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CompList.DataSource = SQLQueries.CompListBindData();
        CompList.DataBind();
    }

    protected void CompList_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button info = e.Row.FindControl("Info") as Button;
            info.Attributes.Add("onclick", "window.showModalDialog('CompDetails.aspx');");

        }
    }


    public void CompList_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "info")
        {
           
            int PCNo = Convert.ToInt32(e.CommandArgument);

            PCNo = Profile.PCID;
        }
    }
Do you think its something to do with the profile??

Stelly
Avatar of gelbert
gelbert

Run your (EXACTLY the same as your application uses) query directly in SQL QueryAnalyzer with parameter (proile ID) which return no output. If you get back no result, then your application is OK and there is just no data in databse for this profile ID
Avatar of stellyuk
stellyuk

ASKER

I have run the SQL query and it works fine it just trying to pass it a value now... i will open a new question for that

Stelly
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo