1
  [WebMethod()]
        public DataTable insert_data_to_db_from_local(string partnumber, string srctcode, string dockcode,int pack,string error,string chk,string user,DateTime day,string ekb,string kbid)
        {
            SqlConnection objConn = new SqlConnection();
            SqlCommand objCmd = new SqlCommand();
            SqlDataAdapter dtAdapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            DataTable dt = null;
            string strConnString = null;
            StringBuilder strSQL = new StringBuilder();
            strConnString = "Server=localhost;UID=sa;PASSWORD=12345678;database=bds_pp_srct;Max Pool Size=400;Connect Timeout=600;";
            strSQL.Append("INSERT INTO Hanheld (Part_Number,SRCT_Code,Dock_Code,Package,Error_Code,Chk_Type,LogUser,LogDate,ekb_order_no,Kanban_ID) VALUES ('" + partnumber + "','" + srctcode + "','" + dockcode + "','" + pack + "','" + error + "','" + chk + "','" + user + "','" + day + "','" + ekb + "','" + kbid + "') ");
            //strSQL.Append(" WHERE [SRCT_Code] = '" + strCusID + "' ");
            objConn.ConnectionString = strConnString;
            var _with1 = objCmd;
            _with1.Connection = objConn;
            _with1.CommandText = strSQL.ToString();
            _with1.CommandType = CommandType.Text;
            dtAdapter.SelectCommand = objCmd;
            dtAdapter.Fill(ds);
            dt = ds.Tables[0];
            dtAdapter = null;
            objConn.Close();
            objConn = null;
            return dt;
        }

This error :

System.IndexOutOfRangeException: Cannot find table 0. at System.Data.DataTableCollection.get_Item(Int32 index)

5
  • 3
    You’re doing an INSERT and use it as it would be a SELECT. It doesn’t return information for you. Commented Aug 21, 2019 at 5:30
  • I'd suggest having a read of stackoverflow.com/questions/14376473/… . Commented Aug 21, 2019 at 5:37
  • Are you trying to insert or select? INSERT Query won't work with this code. Commented Aug 21, 2019 at 5:38
  • how this correct insert code @Znaneswar Commented Aug 21, 2019 at 5:42
  • Check this stackoverflow.com/questions/12939501/… Commented Aug 21, 2019 at 5:44

3 Answers 3

0

Try this one

private DataTable dataTable = new DataTable();
string connString = @"query string here";
string query = "select table";

SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();

SqlDataAdapter da = new SqlDataAdapter(cmd);        
da.Fill(dataTable);
conn.Close();
da.Dispose();
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are using DataSet in your code might be there would be a problem

so you first need to check where that DataSet contains datatable at 0 location

eg.

DataSet ds = new DataSet();
dtAdapter.Fill(ds);
if(ds != null && ds.Tables.Count > 0) {

//your logic

}

Comments

0
[WebMethod()]
    public void insert_data_to_db_from_local(string partnumber, string srctcode, string dockcode)
    {
        using (SqlConnection conn = new SqlConnection("Server=localhost;UID=sa;PASSWORD=12345678;database=Test;Max Pool Size=400;Connect Timeout=600;"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"INSERT INTO Hanheld(Part_Number,SRCT_Code,Dock_Code) VALUES(@partnumber,@srctcode,@dockcode)";
                cmd.Parameters.AddWithValue("@partnumber", partnumber);
                cmd.Parameters.AddWithValue("@srctcode", srctcode);
                cmd.Parameters.AddWithValue("@dockcode", dockcode);

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException e)
                {
                   // MessgeBox.Show(e.Message.ToString(), "Error Message");
                }

            }
        }
    }

This my Be fixed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.