1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

C# sql issue

Discussion in 'C#' started by srisen2, Feb 8, 2011.

  1. #1
    Hi im trying to connect to sql which i can do but i am then trying to use the sql to create a xml file for the output. I am using the below code but the connection is not working.

     protected void saveCurrentData(object sender, EventArgs e)
    {
           SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["cellulant_ConnectionString"].ToString());
        SqlDataAdapter adapt = new SqlDataAdapter(); 
        SqlCommand cmd = new SqlCommand("select * from Product", myConnection); 
        SqlDataAdapter da = new SqlDataAdapter(cmd); 
        DataSet td = new DataSet("Product"); 
        da.Fill(td); 
        td.WriteXml(Server.MapPath("temp\\temp.xml"), XmlWriteMode.WriteSchema);
       
    }
    Code (markup):
    i have the below code connecting and display the sql row i want to insert into the xml just fine

    <asp:GridView
        id="grdProducts"
        DataSourceID="spring11"
        Runat="server" />
    
        
            <asp:SqlDataSource ID="spring11" runat="server" 
            connectionstring="Data Source=xxxxxxx,14330;Initial Catalog=spring11;Persist Security Info=True;User ID=xxxxx; Password= xxxxxx"
               SelectCommand="SELECT * FROM Product">
               </asp:SqlDataSource>
    Code (markup):
    any advice on how to process this
     
    srisen2, Feb 8, 2011 IP
  2. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i am receiving the following error

    error CS0246: The type or namespace name 'SqlConnection' could not be found

    the asp tagged connection in the body works just fine its just the script section which throws this error
     
    srisen2, Feb 8, 2011 IP
  3. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Do you have

    
    using System.Data.SqlClient;
    
    Code (markup):
    at the top of your script file?
     
    hogan_h, Feb 8, 2011 IP
    srisen2 likes this.
  4. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    below is my script its in my main aspx page i think i might be adding this in wrong should i be doing this in the cs file ?

    <script runat="server">
    using System.Data.SqlClient;

    protected void saveCurrentData(object sender, EventArgs e)
    {
    binddata();

    }


    private void binddata()
    {
    SqlConnection sqlConnection = new SqlConnection("Data Source=xxxxxx,14330;Initial Catalog=xxxxxxx;Persist Security Info=True;User ID=xxxxx; Password= xxxxx");
    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select * From Product", sqlConnection);
    DataSet dataSet = new DataSet();
    sqlDataAdapter.Fill(dataSet);
    dataSet.WriteXml(Server.MapPath("temp\\temp.xml"), XmlWriteMode.WriteSchema);
    }

    </script>
     
    srisen2, Feb 8, 2011 IP
  5. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yeah, put it into cs file and see what error do you get. "using ..." directive should go at the top.
     
    hogan_h, Feb 8, 2011 IP
  6. DmitryS

    DmitryS Active Member

    Messages:
    79
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    have you added SQLClient namespace?
     
    DmitryS, Feb 11, 2011 IP
  7. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i got this working by using the cs file and using System.Data.SqlClient;

    Thanks for the help.
     
    srisen2, Feb 14, 2011 IP